在UWP XAML应用程序中使用x:Bind时,请考虑以下事项:
interface IBaseInterface
{
string A { get; set; }
}
interface ISubInterface : IBaseInterface
{
string B { get; set; }
}
class ImplementationClass : ISubInterface
{
public string A { get; set; }
public string B { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在Page类中,我们有以下内容:
public partial class MainPage : Page
{
public ISubClass TheObject = new ImplementationClass { A = "1", B = "2" };
//All the rest goes here
}
Run Code Online (Sandbox Code Playgroud)
在MainPage XAML中,我们有以下代码段:
<TextBlock Text={x:Bind Path=TheObject.A}></TextBlock>
Run Code Online (Sandbox Code Playgroud)
这导致以下编译器错误:XamlCompiler错误WMC1110:无效的绑定路径'A':在'ISubInterface'类型上找不到属性'A'
但是,以下工作正常:
<TextBlock …Run Code Online (Sandbox Code Playgroud) 使用传统的{Binding}语法时,您可以指定元素名称以指向页面上的特定控件,并能够访问其属性.例如,如果页面已命名page您可以执行以下操作:
{Binding ElementName=Page, Path=Name}
Run Code Online (Sandbox Code Playgroud)
用{x:Bind}语法表示
使用x:Bind,您不需要使用ElementName = xxx作为绑定表达式的一部分.使用x:Bind,您可以使用元素的名称作为绑定路径的第一部分,因为命名元素成为页面或用户控件中表示根绑定源的字段.
因此,对于{x:Bind}中的上述示例,将是
{x:Bind page.Name}
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,直到它在数据模板内(例如ListView的ItemTemplate).在这种情况下,它不再工作,因为它正在寻找Page指定的数据类型,这导致以下错误(假设我的数据类型是customer):
XamlCompiler错误WMC1110:无效的绑定路径'Page.Name':在'Customer'类型上找不到属性'Page'
将{x:Bind}语法与数据模板和数据模板外部的访问控制一起使用的解决方案是什么?
我想使用 UWP 的新功能 -> x:Bind。为此,我的所有页面都需要具有 ViewModel 属性(如教程中所述)。为了避免代码重复,我建立了如下基类:
public abstract class BasePage<TBaseVM> : Page, where TBaseVM : BaseVM
{
public TBaseVM VM { get; private set; }
protected BasePage()
{
DataContextChanged += (s, e) => VM = e.NewValue as TBaseVM;
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,此 BasePage 类包含名为“VM”的属性,并且该属性的类型为 BaseVM。因此,我不需要在每个派生类上定义 VM 属性。
然后我创建了在 xaml 中定义的派生页面“MainPage”,如下所示:
<pages:BasePage
x:Class="Realarm.View.Pages.MainPage"
x:TypeArguments="viewModel:MainVM">
Run Code Online (Sandbox Code Playgroud)
通过这样做,即使是 Resharper 的 Intellisense 也为我提供了 MainPage.xaml 中“MainVM”的属性,因此可以这样写:
<ListView ItemsSource="{x:Bind VM.AlarmsVM}">
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我尝试构建项目时,在 MainPage.gics 中出现错误:
严重性代码描述项目文件行错误 CS0305 使用通用类型“BasePage”需要 1 个类型参数 Realarm D:...\Realarm\obj\x86\Debug\View\Pages\MainPage.gics 13
有什么帮助吗?
我正在尝试使用新的x:Bind绑定ListView.SelectedItem.我的代码:
视图:
//MainPage.xaml:
<Page
x:Class="BrokenListSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:BrokenListSample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Beige">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<ListView Grid.Row="0" Background="LawnGreen"
ItemsSource="{x:Bind ViewModel.MyItems, Mode=OneWay}"
SelectedItem="{x:Bind ViewModel.BestItem, Mode=TwoWay}"
Width="300" Height="300"/>
<ListView Grid.Row="1" Background="LawnGreen"
ItemsSource="{Binding MyItems, Mode=OneWay}"
SelectedItem="{Binding BestItem, Mode=TwoWay}"
Width="300" Height="300"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
代码隐藏:
//MainPage.xaml.cs:
using Windows.UI.Xaml.Controls;
namespace BrokenListSample
{
public sealed partial class MainPage : Page
{
public MainPageViewModel ViewModel { get; set; }
public MainPage()
{
InitializeComponent();
DataContextChanged += (s, e) => { ViewModel = DataContext as MainPageViewModel; };
DataContext = …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建我的UWP应用程序,当前尝试使用DataTemplate与x:Bind在资源字典中时,我遇到了设计器异常.
我创建了一个资源字典"ItemTemplates.xaml",其中包含相应的代码隐藏(以确保x:Bind初始化).该文件只包含一个模板:
<DataTemplate x:Key="HomeViewCategoryListItemTemplate" x:DataType="models:Category">
<Button Background="#88333333" Height="110" VerticalContentAlignment="Top" Padding="10" HorizontalAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock FontWeight="Light" HorizontalAlignment="Center" Text="{x:Bind Name}" FontSize="{ThemeResource TextStyleExtraLargeFontSize}" />
<TextBlock Foreground="{ThemeResource ToolTipForegroundThemeBrush}" HorizontalAlignment="Center" Margin="0,10,0,0" Text="{x:Bind Description}" Grid.Row="1" TextAlignment="Center" TextWrapping="Wrap" />
</Grid>
</Button>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
然后我将这个资源字典添加到App.xaml,如下所示:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Resources/Core.xaml" />
<resources:ItemTemplates />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
现在项目无法使用,因为设计师抛出奇怪的异常,但是当我清理并重建项目并导航到HomeView.xaml页面时,设计器只显示默认的"ToString()"项(基本上列表视图只包含三次) ListView中的文本"Models.Categories"和ListView的ItemTemplate属性带有下划线并显示以下错误:
The resource "HomeViewCategoryListItemTemplate" could not be resolved.
Run Code Online (Sandbox Code Playgroud)
当我导航回App.xaml时,我看到另一个下划线(该<resources:ItemTemplates />行):
The property 'DataType' was not found in type 'DataTemplate'.
Run Code Online (Sandbox Code Playgroud)
这两个错误都是非敏感的,因为当我实际运行应用程序时,没有任何问题,一切都运行良好.到目前为止,我发现的唯一解决方法是以经典方式和"编译"方式包括ResourceDictionary两次:
<ResourceDictionary …Run Code Online (Sandbox Code Playgroud) 我读到x:Bind,它比Binding. 但在我的应用程序 WPF C# with .NET Framework 4.8 中,当我放入x:Bind任何部分 ( TextBox Text="{x:Bind ...}") 时,Visual Studio 会对我说“Windows Presentation Foundation (WPF) 项目不支持绑定”。
您不可能在 WPF C# 桌面应用程序中 x:Bind 吗?或者我需要做什么才能使用它?
我在程序中使用Hubsection,并且<HubSection>有一个ListView。但我无法将数据绑定到ListView。我曾经尝试使用,{binding}但是在输出中我变得空白,而在使用时x:bind我得到的错误是
没有为DataTemplate定义DataType。包含x:Bind的模板需要使用'x:DataType'指定数据类型
帮助我解决这个问题。这是我的代码:
.xaml
<Hub Header="abc" FontWeight="Bold">
<HubSection Header="header1" x:Name="hub1">
<DataTemplate >
<ListView x:Name="list1" ItemsSource="{x:Bind info}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Image Source="{X:Bind image}"></Image>
<TextBlock Text="{x:Bind name}"/>
</StackPanel>
<TextBlock Text="{x:Bind bio}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</HubSection>
<HubSection Header="header 2">
<DataTemplate>
</DataTemplate>
</HubSection>
</Hub>
</Grid>
Run Code Online (Sandbox Code Playgroud)
.cs
namespace app1
{
public class info
{
public String name { get; set; }
public String bio { get; set; }
public String image …Run Code Online (Sandbox Code Playgroud) 使用{x:Bind}标记语法,您可以绑定到事件,前提是该方法满足以下要求:
这在DataTemplate之外完美地运行.一旦绑定发生在DataTemplate内部,编译器就会生成以下错误:
Xaml内部错误错误WMC9999:对象引用未设置为对象的实例.
绑定到DataTemplates内事件的修复方法是什么?
完整的示例代码在这里.
以下示例代码的片段 - 注意第一个按钮(第2行)很好,第二个按钮(第6行)也很好.如果您注释掉第6行并在第7行注释,则会发生错误.
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button Tapped="{x:Bind Click}" Content="WORKING"/>
<ListView ItemsSource="{x:Bind Names}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Customer">
<Button Content="{x:Bind Title}"/>
<!--<Button Tapped="{x:Bind Clicky}" Content="{x:Bind Title}"/>-->
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
Run Code Online (Sandbox Code Playgroud) 我想在 UWP 中使用 ResourceDictionary 就像我在 WPF 中使用的那样在 WPF 中,我在 ResourceDictionary 文件(*Style.xaml)中执行此操作
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:models="using:NWP.Models">
<Style x:key="MenuContent" TargetType="ContentControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<controls:DockPanel>
<ItemsControl ItemsSource="{x:Bind How-Can-I-Bind-Collection-Here?}">
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="models:MenuItemModel">
<RadioButton GroupName="MenuItems" IsChecked="{x:Binding IsChecked, Mode=TwoWay}" MinHeight="0" MinWidth="0" Command="{Binding Command}" CommandParameter="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentTransitions="{TemplateBinding ContentTransitions}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</controls:DockPanel>
</ControlTemplate>
</Setter.Value>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
然后我可以在我的页面中使用这种样式:
<ContentControl Style="{StaticResource MenuContent}">
<StackPanel Orientation="Vertical">
<TextBox/>
<PasswordBox/>
<Button Content="Login"/>
</StackPanel>
</ContentControl> …Run Code Online (Sandbox Code Playgroud) 我不知道为什么他们决定为设置默认值Mode来OneTime但是,这不是我想要的大部分时间.我浪费了一整天的调试时间.
有没有一种方法来设置OneWay值作为默认Mode的x:Bind?
<!--This will not listen to future changes. it is OneTime by default-->
<TextBlock Text="{x:Bind Name}"/>
<!--We have to explicitly define Mode value-->
<TextBlock Text="{x:Bind Name, Mode=OneWay}"/>
Run Code Online (Sandbox Code Playgroud) 我正在努力掌握已编译的数据绑定概念。我有一个视图 (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 的资源中,它也可以正常工作。我错过了什么?
xbind ×11
uwp ×6
xaml ×6
binding ×4
c# ×4
mvvm ×2
windows-10 ×2
winrt-xaml ×2
datatemplate ×1
designer ×1
generics ×1
listview ×1
wpf ×1