UWP当我发现标记功能的新功能时,使用了标记扩展,带来了一种新的方式DataBinding,发现我们实际上可以将事件绑定到方法上!Compiled Binding{x:Bind}
范例:
Xaml:
<Grid>
<Button Click="{x:Bind Run}" Content="{x:Bind ButtonText}"></Button>
</Grid>
Run Code Online (Sandbox Code Playgroud)
背后的代码:
private string _buttonText;
public string ButtonText
{
get { return _buttonText; }
set
{
_buttonText = value;
OnPropertyChanged();
}
}
public MainPage()
{
this.InitializeComponent();
ButtonText = "Click !";
}
public async void Run()
{
await new MessageDialog("Yeah the binding worked !!").ShowAsync();
}
Run Code Online (Sandbox Code Playgroud)
结果 :
而且由于{x:Bind}绑定是在运行时评估的,并且编译器会生成一些表示该绑定的文件,所以我去那里研究了发生了什么,因此在MainPage.g.cs文件中(MainPage是有问题的xaml文件) ) 我找到了这个 :
// IComponentConnector
public void Connect(int connectionId, global::System.Object target)
{
switch(connectionId)
{
case 2:
this.obj2 = …Run Code Online (Sandbox Code Playgroud) 我正在尝试掌握新的编译绑定,但是一开始我就被这个简单的问题阻止了。
我有Hub一个控制HubSection。本节的内容是ItemsControl需要绑定到视图模型的可观察集合。我无法让这个绑定像我期望的那样工作。
<Pivot x:Name="rootPivot" Style="{StaticResource TabsStylePivotStyle}">
<PivotItem>
<Hub>
<HubSection Header="News">
<DataTemplate x:DataType="local:HomePage">
<ItemsControl ItemsSource="{x:Bind ViewModel.NewsItems, Mode=OneWay}" />
Run Code Online (Sandbox Code Playgroud)
ViewModelproperty 只是一个属性,在InitializeComponents()调用之前被实例化。NewsItems是在页面加载后填充的视图模型中的可观察集合 - 异步(Web 请求)。
我在这里做错了什么?
编辑:代码隐藏
主页.xaml.cs
/// <summary>
/// Home pag view.
/// </summary>
public sealed partial class HomePage : Page
{
/// <summary>
/// Initializes a new instance of the <see cref="HomePage"/> class.
/// </summary>
public HomePage()
{
// Retrieve view model
this.ViewModel = ViewModelResolver.Home;
// Trigger view model …Run Code Online (Sandbox Code Playgroud) 我根据这篇文章制作了"MVVM flyout":https://marcominerva.wordpress.com/2015/01/15/how-to-open-and-close-flyouts-in-universal-apps-using-mvvm/
它运作良好.但它不适用于编译绑定(x:绑定)
这个:
<Flyout local:FlyoutHelpers.Parent="{x:Bind ShowButton}"...
Run Code Online (Sandbox Code Playgroud)
这个问题:
<Flyout local:FlyoutHelpers.Parent="{Binding ElementName=ShowButton}"...
Run Code Online (Sandbox Code Playgroud)
在构建时抛出奇怪的错误:
错误CS1503参数1:无法从"Windows.UI.Xaml.Controls.Flyout"转换为"Windows.UI.Xaml.FrameworkElement"
有没有选项如何使用x:Bind?
我正在努力掌握已编译的数据绑定概念。我有一个视图 (MainPage),其中包含一个 ListBox 和一个用于该 ListBox 的数据模板 (ItemTemplate)。MainPage 有一个 MainPageViewModel,其中包含一个 ObservableCollection 的 ItemViewModel。ItemViewModel 仅包含一个属性名称。
主页:
<Page x:Class="TestApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestApp">
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ItemDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListBox ItemsSource="{x:Bind ViewModel.Items}"
ItemTemplate="{StaticResource ItemTemplate}" />
</Grid>
</Page>
Run Code Online (Sandbox Code Playgroud)
包含数据模板的资源字典:
<ResourceDictionary
x:Class="TestApp.ItemDictionary"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestApp">
<DataTemplate x:Key="ItemTemplate" x:DataType="local:ItemViewModel">
<TextBlock Text="{x:Bind Name}" />
</DataTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
这段代码可以编译,但是当我运行它时,与 Name 属性的绑定失败,尽管生成了项目。如果我使用经典绑定,一切正常,如果我将数据模板直接放在 MainPage 的资源中,它也可以正常工作。我错过了什么?
我创建了一个名为 的模型类Project,还创建了一个名为 的 ViewModel 类MainPageViewModel。
我想要实现的是一个具有多列的简单表。应该有一个名为“操作”的列,并且该列应该有一个按钮。
这是 XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SharpTool.UI.MainPage"
xmlns:viewmodel="clr-namespace:SharpTool.UI.ViewModels"
xmlns:models ="clr-namespace:SharpTool.UI.Models"
x:DataType="viewmodel:MainPageViewModel">
<ScrollView>
<VerticalStackLayout
Padding="10, 10">
<Border MinimumHeightRequest="50" StrokeThickness ="0.5">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border StrokeThickness="0.1" Grid.Column="0">
<Label HorizontalOptions="Center" VerticalOptions="Center" FontFamily="icon" FontSize="15" Text="Id" TextColor="Blue"/>
</Border>
<Border StrokeThickness="0.1" Grid.Column="1">
<Label HorizontalOptions="Center" VerticalOptions="Center" FontFamily="icon" FontSize="15" Text="Name" TextColor="Blue"/>
</Border>
<Border StrokeThickness="0.1" Grid.Column="2">
<Label HorizontalOptions="Center" VerticalOptions="Center" FontFamily="icon" FontSize="15" Text="Discription" TextColor="Blue"/>
</Border>
<Border …Run Code Online (Sandbox Code Playgroud)