XAML Xamarin中的RelativeSource绑定出现意外的ReSharper警告

iqih9akk  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(116)

在下面的代码片段中,我收到了ReShaper的警告
“无法解析”object“类型的数据上下文中的属性”MyCommand“”
我有以下问题

  • 当我想从视图模型中执行一个命令,并使用一个来自ItemSource的参数时,这是正确的方法吗
  • 为什么我得到这个警告?
  • 我如何才能摆脱这个警告?
  • 如果没有,我可以安全地忽略警告吗

所以代码按预期工作,但我担心警告
这不是我的代码,但我提取了一个简化的片段,以简化,以重现问题
这是我的代码
XAML

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:app1="clr-namespace:App1;assembly=App1"
             x:Class="App1.MainPage">
    <ContentPage.BindingContext>
        <app1:MainViewModel></app1:MainViewModel>
    </ContentPage.BindingContext>
    <ContentPage.Resources>
        <DataTemplate x:Key="Template">
            <Button Text="{Binding}" 
                    Command="{Binding Path=BindingContext.MyCommand, Source={RelativeSource AncestorType={x:Type CollectionView}} }" 
                    CommandParameter="{Binding}">
            </Button>

        </DataTemplate>
    </ContentPage.Resources>

    <StackLayout>
        <CollectionView ItemsSource="{Binding Items}" ItemTemplate="{StaticResource Template}">

        </CollectionView>
    </StackLayout>

</ContentPage>

字符串
ViewModel

using System.Collections.ObjectModel;
using Xamarin.Forms;

namespace App1
{
    public class MainViewModel
    {
        public ObservableCollection<string> Items { get; set; }= new ObservableCollection<string>() {"1", "2", "3"} ;

        public Command<string> MyCommand { get; set; }

    }
}


xaml.cs

using Xamarin.Forms;

namespace App1
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            MainViewModel.MyCommand = new Command<string>(ShowMessage);
        }

        private MainViewModel MainViewModel => (MainViewModel)BindingContext;

        private void ShowMessage(string message)
        {
            DisplayAlert("Information", message, "OK");
        }

    }
}


我尝试以不同的方式进行绑定,但代码不再起作用。例如,我将Source={RelativeSource RestorType ={x:Type CollectionView}}更改为Source={x:Reference MyPage,但仍然收到警告

6rvt4ljy

6rvt4ljy1#

您可以通过正确设置编译绑定来解决这个问题。就像Jason已经建议的那样,您应该在 ContentPage 根目录和 DataTemplate 上使用x:DataType属性。
然而,你也需要稍微不同地设置绑定。而不是绑定到BindingContextCollectionView作为源,你应该直接绑定到ViewModel:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:app1="clr-namespace:App1;assembly=App1"
             x:Class="App1.MainPage"
             x:DataType="app1:MainViewModel">
    <ContentPage.BindingContext>
        <app1:MainViewModel></app1:MainViewModel>
    </ContentPage.BindingContext>
    <ContentPage.Resources>
        <DataTemplate x:Key="Template"
                      x:DataType="{x:Type System:String}">
            <Button Text="{Binding}" 
                    Command="{Binding MyCommand, Source={RelativeSource AncestorType={x:Type app1:MainViewModel}} }" 
                    CommandParameter="{Binding}">
            </Button>

        </DataTemplate>
    </ContentPage.Resources>

    <StackLayout>
        <CollectionView ItemsSource="{Binding Items}" ItemTemplate="{StaticResource Template}">

        </CollectionView>
    </StackLayout>

</ContentPage>

字符串

8yoxcaq7

8yoxcaq72#

XAML编译器不知道您绑定到什么类型,因此无法预编译。您可以使用DataType参数解决此问题
在页面中添加一个以设置VM类型

<ContentPage ...
 x:DataType="app1:MainViewModel"
 ... >

字符串
另一个绑定到DataTemplate,这样它就知道它绑定到的类型

<DataTemplate x:Key="Template"
 x:DataType="{x:Type System:String}" >

相关问题