我正在使用帶有 ItemTemplateSelector 的可綁定堆棧布局。我的數據模板位于另一個文件中,該文件是作為 MergedResourceDictionay 包含在 MainView 中的 ResourceDictionary。在我的一個 DataTemplates 中,我有帶有 TapGestureRecognizer 的標簽,它應該在 MainViewViewModel 中觸發命令,但我似乎無法開始工作......我嘗試Source={x:Reference MainPage}在我的命令綁定中使用它,但無法引用它,因為它不在同一個文件中(Xamarin.Forms.Xaml.XamlParseException:“位置 28:51。找不到 MainPage 引用的對象”)<--! this is snippet from MainPage --> <ScrollView Orientation="Vertical" Grid.Row="1"> <local:BindableStackLayout BindableLayout.ItemsSource="{Binding Day.TodayEntry}" x:Name="BindableStack" Spacing="10" Margin="10" BindableLayout.ItemTemplateSelector="{StaticResource CardDetailTemplateSelector}"/> </ScrollView><--! this is problematic snippet from data template --><Label Text="REMOVE" FontSize="Medium" TextColor="White" HorizontalOptions="End" Margin="3,0,0,0"> <Label.GestureRecognizers> <TapGestureRecognizer Command="{Binding RemoveEntryCommand, Source={x:Reference MainPage}}" CommandParameter="{Binding .}"/> </Label.GestureRecognizers></Label>
1 回答

慕俠2389804
TA貢獻1719條經驗 獲得超6個贊
不起作用的原因是 XAML 編譯查找是特定于頁面的,這意味著如果您有兩個不同的頁面并且使用名稱進行搜索,則在大多數情況下它將不起作用。
在這種情況下,我通常所做的就是為 BindingContext 使用parent關鍵字!
因此,假設您的標簽(位于 ViewCell 中)的父級是 Grid,您可以執行類似的操作
Command="{Binding Source={x:Reference parentLayoutGrid}, Path=Parent.Parent.BindingContext.RemoveEntryCommand}"
所需的父屬性數量取決于您的 ViewHeirarchy 以及哪個 View 將具有正確的上下文。
祝你好運。
如有疑問,請隨時回復
- 1 回答
- 0 關注
- 147 瀏覽
添加回答
舉報
0/150
提交
取消