xamarin 可绑定布局>绑定:在ViewModel上找不到属性X

dhxwm5r4  于 2023-08-01  发布在  其他
关注(0)|答案(3)|浏览(112)

我有一个简单的XAML页面,代码如下:

<StackLayout BindableLayout.ItemsSource="{Binding Data}">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <Label BackgroundColor="{Binding Color}"
                   Text="{Binding Text}"
                   FontAttributes="Italic"
                   FontSize="20"
                   VerticalTextAlignment="Center"
                   HorizontalTextAlignment="Center"
                   HeightRequest="105"
                   Margin="25" />
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

字符串
在查看页面代码后面我有:

public partial class DataView
    {
        public DataView()
        {
            InitializeComponent();

            BindingContext = new ViewModel();
        }
...
}


数据为:

public class ViewModel
{
    public ViewModel()
    {
        Data = new ObservableCollection()<Model> { 
            new Model { Text = "Pink", Color = Color.DeepPink }, 
            new Model { Text = "Crimson", Color = Color.Crimson }, 
            new Model { Text = "Aqua", Color = Color.Aqua }, 
            new Model { Text = "Blue", Color = Color.DeepSkyBlue }, 
            new Model { Text = "BurlyWood", Color = Color.BurlyWood }, };
    }
 
    public ObservableCollection<Model> Data { get; set; }
}
 
public class Model
{
    public Color Color { get; set; }
    public string Text { get; set; }
}


但是,在编译时,我得到:
绑定:在“MyComp.ViewModel”上未找到属性“Color”。
看起来它在ViewModel中搜索Color而不是parent(Data)。
我正在使用最新的Xamarin.Forms 4.8.0,.NET Standard 2.0和Visual Studio 2019 16.7.4。

z4iuyo4d

z4iuyo4d1#

我测试了xaml代码,它仍然工作正常。检查gif:https://us.v-cdn.net/5019960/uploads/editor/pd/aa0fp5pwnvkl.gif
这里是相关代码,大家可以参考一下。
Page.xaml

<StackLayout x:Name="expanderLayout" BindableLayout.ItemsSource="{Binding Data}">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <Expander>
                <Expander.Header>
                    <Grid>
                        <Label BackgroundColor="{Binding Color}"/>
                    </Grid>
                </Expander.Header>
                <Expander.ContentTemplate>
                    <DataTemplate>
                        <Label
                            Text="{Binding Text}"
                            FontAttributes="Italic"
                            FontSize="20"
                            VerticalTextAlignment="Center"
                            HorizontalTextAlignment="Center"
                            HeightRequest="105"
                            Margin="25" />
                    </DataTemplate>
                </Expander.ContentTemplate>
            </Expander>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>

字符串
Page.xaml.cs

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        BindingContext = new ViewModel();
    }
}


Model类和ViewModel类

public class ViewModel
{
    public ViewModel()
    {
        Data = new System.Collections.ObjectModel.ObservableCollection<Model>() {
                new Model { Text = "Pink", Color = Color.DeepPink },
            new Model { Text = "Crimson", Color = Color.Crimson },
            new Model { Text = "Aqua", Color = Color.Aqua },
            new Model { Text = "Blue", Color = Color.DeepSkyBlue },
            new Model { Text = "BurlyWood", Color = Color.BurlyWood }, };
    }

    public ObservableCollection<Model> Data { get; set; }
}

public class Model
{
    public Color Color { get; set; }
    public string Text { get; set; }
}

8qgya5xd

8qgya5xd2#

我试着运行你的代码,看到它没有什么问题,除了一些轻微的错别字在你的问题,我有它作为这个

public ViewModel()
    {
        Data = new ObservableCollection<Model>
        {
            new Model {Text = "Pink", Color = Color.DeepPink},
            new Model {Text = "Crimson", Color = Color.Crimson},
            new Model {Text = "Aqua", Color = Color.Aqua},
            new Model {Text = "Blue", Color = Color.DeepSkyBlue},
            new Model {Text = "BurlyWood", Color = Color.BurlyWood},
        };
    }

字符串
,也许可以尝试清理/重建解决方案并再次运行?
x1c 0d1x的数据

7vux5j2d

7vux5j2d3#

要在此结束循环...正如@MilanM在10-17-20上评论的那样,他通过从xaml中删除这个来使它工作

x:DataType="local:ViewModel"

字符串
我想其他人会说代码运行得很好,因为他们的xaml中从来没有这一行。@MilanM在他发表评论之前从未展示过。
我从我的xaml中删除了2行,然后一切正常。

xmlns:viewModel="clr-namespace:MauiApp1.ViewModels"
x:DataType="viewModel:MainViewModel"


注意:我在代码隐藏中使用以下内容设置了BindingContext:

public MainPage(MainViewModel _vm)
{
    InitializeComponent();
    BindingContext = _vm;
}


高温高压

相关问题