简体   繁体   English

投射绑定路径,以便在设计时识别 ViewModel 属性

[英]Cast Binding Path so it recognises ViewModel property at Design-Time

Ok this is more of an annoyance than a problem.好吧,这比问题更令人烦恼。 There is no error没有错误

Page

<ContentPage
   ...
   x:Name="This"
   //hack to have typed xaml at design-time
   BindingContext="{Binding Source={x:Static viewModels:ViewModelLocator.ChooseTargetLocationVm}}"

SubView子视图

<views:ProductStandardView
    ...
    BindingContext="{Binding Product}">
    <Grid.Triggers>
        <DataTrigger
            Binding="{Binding Path=BindingContext.IsVacate, Source={x:Reference This}}"
            TargetType="Grid"
            Value="true">
            <Setter Property="BackgroundColor" Value="{StaticResource WarningColor}" />
        </DataTrigger>
    </Grid.Triggers>

When Binding to BindingContext from the Source Reference of This , i get a XAML "warning"当从This引用绑定BindingContext ,我收到 XAML“警告”

Cannot resolve property 'IsVacate' in data context of type 'object'无法在“对象”类型的数据上下文中解析属性“IsVacate”

Binding="{Binding Path=BindingContext.IsVacate, Source={x:Reference This}}"

Obviously the BindingContext is an object and untyped.显然BindingContext是一个对象并且是无类型的。 However the above code compiles and works但是上面的代码编译并工作

What i want to do is cast it, firstly because i have OCD, however mainly because its easy to spot real problems on the IDE page channel bar我想做的是投射它,首先是因为我有强迫症,但主要是因为它很容易在 IDE 页面通道栏上发现真正的问题

The following seems logical but doesn't work以下似乎合乎逻辑但不起作用

Binding="{Binding Path=BindingContext.(viewModels:ChooseTargetLocationVm.IsVacate), 
                  Source={x:Reference This}}"

In the output i get在输出中我得到

[0:] Binding: '( viewModels:ChooseTargetLocationVm ' property not found on ' Inhouse.Mobile.Standard.ViewModels.ChooseTargetLocationVm ', target property: ' Inhouse.Mobile.Standard.Views.ProductStandardView.Bound ' [0:] 绑定:'( viewModels:ChooseTargetLocationVm '属性未在' Inhouse.Mobile.Standard.ViewModels.ChooseTargetLocationVm '上找到,目标属性:' Inhouse.Mobile.Standard.Views.ProductStandardView.Bound '

I understand the error, yet how else would i cast?我明白这个错误,但我还能如何施展?


And just for stupidity, obviously the following wont compile只是为了愚蠢,显然以下不会编译

Binding="{Binding Path=((viewModels:ChooseTargetLocationVm)BindingContext).IsVacate, Source={x:Reference This}}"

So is there a way to cast a BindingContext to a ViewModel so any SubProperty references are typed at design time?那么有没有办法将BindingContext转换为ViewModel,以便在设计时输入任何SubProperty引用?

Update更新

This is relevant for inside a DataTemplate or in this case when the control has its own BindingContext which is why i need to use the Source={x:Reference This} to target the page.这与DataTemplate内部相关,或者在这种情况下,当控件具有自己的BindingContext ,这就是为什么我需要使用Source={x:Reference This}来定位页面的原因。

Note : <ContentPage.BindingContext> doesn't work for me as i'm using prism and unity and it doesn't seem to play with well a default constructor on initial tests, though i might play around with this some more注意<ContentPage.BindingContext>对我不起作用,因为我正在使用棱镜和统一,并且它似乎在初始测试中与默认构造函数一起使用效果不佳,尽管我可能会更多地使用它

You can extend ContentPage to create a generic type - that supports type parameter for view-model - which in turn can be used in Binding markup extension.您可以扩展ContentPage以创建通用类型 - 支持视图模型的类型参数 - 反过来可以在Binding标记扩展中使用。

Although it may not give you intellisense like support - but should definitely remove the warning for you.虽然它可能不会为您提供类似智能感知的支持 - 但绝对应该为您删除警告。

For eg:例如:

/// <summary>
/// Create a base page with generic support
/// </summary>
public class ContentPage<T> : ContentPage
{
    /// <summary>
    /// This property basically type-casts the BindingContext to expected view-model type
    /// </summary>
    /// <value>The view model.</value>
    public T ViewModel { get { return (BindingContext != null) ? (T)BindingContext : default(T); } }

    /// <summary>
    /// Ensure ViewModel property change is raised when BindingContext changes
    /// </summary>
    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();

        OnPropertyChanged(nameof(ViewModel));
    }
}

Sample usage示例用法

<?xml version="1.0" encoding="utf-8"?>
<l:ContentPage 
    ...
    xmlns:l="clr-namespace:SampleApp" 
    x:TypeArguments="l:ThisPageViewModel"
    x:Name="This"
    x:Class="SampleApp.SampleAppPage">

    ...                            
         <Label Text="{Binding ViewModel.PropA, Source={x:Reference This}}" />
    ...
</l:ContentPage>

Code-behind代码隐藏

public partial class SampleAppPage : ContentPage<ThisPageViewModel>
{
    public SampleAppPage()
    {
        InitializeComponent();

        BindingContext = new ThisPageViewModel();
    }
}

View model查看模型

/// <summary>
/// Just a sample viewmodel with properties
/// </summary>
public class ThisPageViewModel
{
    public string PropA { get; } = "PropA";
    public string PropB { get; } = "PropB";
    public string PropC { get; } = "PropC";

    public string[] Items { get; } = new[] { "1", "2", "3" };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM