我正在使用帶有 ItemTemplateSelector 的可綁定堆棧布局。我的數(shù)據(jù)模板位于另一個(gè)文件中,該文件是作為 MergedResourceDictionay 包含在 MainView 中的 ResourceDictionary。在我的一個(gè) DataTemplates 中,我有帶有 TapGestureRecognizer 的標(biāo)簽,它應(yīng)該在 MainViewViewModel 中觸發(fā)命令,但我似乎無(wú)法開始工作......我嘗試Source={x:Reference MainPage}在我的命令綁定中使用它,但無(wú)法引用它,因?yàn)樗辉谕粋€(gè)文件中(Xamarin.Forms.Xaml.XamlParseException:“位置 28:51。找不到 MainPage 引用的對(duì)象”)<--! 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貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊
不起作用的原因是 XAML 編譯查找是特定于頁(yè)面的,這意味著如果您有兩個(gè)不同的頁(yè)面并且使用名稱進(jìn)行搜索,則在大多數(shù)情況下它將不起作用。
在這種情況下,我通常所做的就是為 BindingContext 使用parent關(guān)鍵字!
因此,假設(shè)您的標(biāo)簽(位于 ViewCell 中)的父級(jí)是 Grid,您可以執(zhí)行類似的操作
Command="{Binding Source={x:Reference parentLayoutGrid}, Path=Parent.Parent.BindingContext.RemoveEntryCommand}"
所需的父屬性數(shù)量取決于您的 ViewHeirarchy 以及哪個(gè) View 將具有正確的上下文。
祝你好運(yùn)。
如有疑問,請(qǐng)隨時(shí)回復(fù)
- 1 回答
- 0 關(guān)注
- 142 瀏覽
添加回答
舉報(bào)
0/150
提交
取消