x:Bind 并嵌套在 UWP 应用程序中 class
x:Bind and nested class in a UWP application
我目前正在开发 UWP 应用程序,我想使用已编译的绑定系统。
我有一个扩展 Windows.UI.Xaml.Controls.Page
的基础,其中包含 属性 ViewModel
.
这里是 ViewModel
属性 的基数 class :
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaiseOnPropertyChanged([CallerMemberName] string propertyName = "")
{
OnPropertyChanged(propertyName);
}
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
这是我的基本页面:
public abstract class BasePage : Page
{
public BaseViewModel ViewModel { get; set; }
}
我的应用程序的页面扩展了 BasePage
并包含一个扩展 BaseViewModel
class 的嵌套(内部)class。这里有一个示例代码:
public sealed partial class MyPage : BasePage
{
public sealed class MyViewModel : BaseViewModel
{
public string Title
{
get
{
return "Test";
}
}
}
public MyPage()
{
InitializeComponent();
ViewModel = MyViewModel();
}
}
现在,我想将 MyViewModel
class 的 属性 Title
绑定到我的 UI。根据 this article and this one,类似的东西应该可以工作:
<TextBlock
Text="{x:Bind ViewModel.(namespace:MyPage+MyViewModel.Title)}"
/>
不幸的是,我无法编译。由于“+”字符,我在生成的文件 MyPage.g.cs
上有几个错误。您知道 UWP 应用程序是否支持嵌套(内部)class 上的绑定吗?也许它仅在 WPF 应用程序上受支持? :(
感谢您的帮助!
在 {x:Bind}
中使用“+”字符进行转换时出现一些问题。在 MyPage.xaml 中使用 {x:Bind ViewModel.(local:MyPage+MyViewModel.Title)}
时,它会在 MyPage.g.cs 中生成如下代码来更新绑定数据:
private void Update_ViewModel(global::UWP.BaseViewModel obj, int phase)
{
if (obj != null)
{
if ((phase & (NOT_PHASED | (1 << 0))) != 0)
{
this.Update_ViewModel__local_MyPage+MyViewModel_Title_(((global::UWP.MyPage.MyViewModel)(obj)).Title, phase);
}
}
}
private void Update_ViewModel__local_MyPage+MyViewModel_Title_(global::System.String obj, int phase)
{
if((phase & ((1 << 0) | NOT_PHASED )) != 0)
{
XamlBindingSetters.Set_Windows_UI_Xaml_Controls_TextBlock_Text(this.obj2, obj, null);
}
}
虽然生成的cast是对的,但是方法名Update_ViewModel__local_MyPage+MyViewModel_Title_
是无效的。因此现在不支持在 {x:Bind}
中投射嵌套 class。
如果你想使用{x:Bind}
,你可以使用MyViewModel
代替下面的BaseViewModel
,它会很好用。
在代码隐藏中:
public sealed partial class MyPage : BasePage
{
public MyViewModel myViewModel;
public sealed class MyViewModel : BaseViewModel
{
public string Title
{
get
{
return "Test";
}
}
}
public MyPage()
{
InitializeComponent();
myViewModel = new MyViewModel();
}
}
在XAML:
<TextBlock Text="{x:Bind myViewModel.Title}" />
此外,如果您想使用 BaseViewModel
,您可以使用 {Binding}
而不是 {x:Bind}
,因为 {Binding}
使用通用运行时对象检查。
在代码隐藏中:
public sealed partial class MyPage : BasePage
{
public sealed class MyViewModel : BaseViewModel
{
public string Title
{
get
{
return "Test";
}
}
}
public MyPage()
{
InitializeComponent();
ViewModel = new MyViewModel();
this.DataContext = this;
}
}
在XAML:
<TextBlock Text="{Binding ViewModel.Title}" />
解决方案是使用新的 属性 以避免转换和嵌套 class 访问 XAML 文件。所以解决方案是使用类似的东西:
public sealed partial class MainPage : BasePage
{
public sealed class MyViewModel : BaseViewModel
{
public string Title
{
get
{
return "Test";
}
}
}
public MyViewModel LocalViewModel
{
get
{
return (MyViewModel) ViewModel;
}
}
public MainPage()
{
this.InitializeComponent();
ViewModel = new MyViewModel();
}
}
因此,使用 x:Bind
语法,XAML 看起来像:
<TextBlock Text="{x:Bind LocalViewModel.Title}" />
我目前正在开发 UWP 应用程序,我想使用已编译的绑定系统。
我有一个扩展 Windows.UI.Xaml.Controls.Page
的基础,其中包含 属性 ViewModel
.
这里是 ViewModel
属性 的基数 class :
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaiseOnPropertyChanged([CallerMemberName] string propertyName = "")
{
OnPropertyChanged(propertyName);
}
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
这是我的基本页面:
public abstract class BasePage : Page
{
public BaseViewModel ViewModel { get; set; }
}
我的应用程序的页面扩展了 BasePage
并包含一个扩展 BaseViewModel
class 的嵌套(内部)class。这里有一个示例代码:
public sealed partial class MyPage : BasePage
{
public sealed class MyViewModel : BaseViewModel
{
public string Title
{
get
{
return "Test";
}
}
}
public MyPage()
{
InitializeComponent();
ViewModel = MyViewModel();
}
}
现在,我想将 MyViewModel
class 的 属性 Title
绑定到我的 UI。根据 this article and this one,类似的东西应该可以工作:
<TextBlock
Text="{x:Bind ViewModel.(namespace:MyPage+MyViewModel.Title)}"
/>
不幸的是,我无法编译。由于“+”字符,我在生成的文件 MyPage.g.cs
上有几个错误。您知道 UWP 应用程序是否支持嵌套(内部)class 上的绑定吗?也许它仅在 WPF 应用程序上受支持? :(
感谢您的帮助!
在 {x:Bind}
中使用“+”字符进行转换时出现一些问题。在 MyPage.xaml 中使用 {x:Bind ViewModel.(local:MyPage+MyViewModel.Title)}
时,它会在 MyPage.g.cs 中生成如下代码来更新绑定数据:
private void Update_ViewModel(global::UWP.BaseViewModel obj, int phase)
{
if (obj != null)
{
if ((phase & (NOT_PHASED | (1 << 0))) != 0)
{
this.Update_ViewModel__local_MyPage+MyViewModel_Title_(((global::UWP.MyPage.MyViewModel)(obj)).Title, phase);
}
}
}
private void Update_ViewModel__local_MyPage+MyViewModel_Title_(global::System.String obj, int phase)
{
if((phase & ((1 << 0) | NOT_PHASED )) != 0)
{
XamlBindingSetters.Set_Windows_UI_Xaml_Controls_TextBlock_Text(this.obj2, obj, null);
}
}
虽然生成的cast是对的,但是方法名Update_ViewModel__local_MyPage+MyViewModel_Title_
是无效的。因此现在不支持在 {x:Bind}
中投射嵌套 class。
如果你想使用{x:Bind}
,你可以使用MyViewModel
代替下面的BaseViewModel
,它会很好用。
在代码隐藏中:
public sealed partial class MyPage : BasePage
{
public MyViewModel myViewModel;
public sealed class MyViewModel : BaseViewModel
{
public string Title
{
get
{
return "Test";
}
}
}
public MyPage()
{
InitializeComponent();
myViewModel = new MyViewModel();
}
}
在XAML:
<TextBlock Text="{x:Bind myViewModel.Title}" />
此外,如果您想使用 BaseViewModel
,您可以使用 {Binding}
而不是 {x:Bind}
,因为 {Binding}
使用通用运行时对象检查。
在代码隐藏中:
public sealed partial class MyPage : BasePage
{
public sealed class MyViewModel : BaseViewModel
{
public string Title
{
get
{
return "Test";
}
}
}
public MyPage()
{
InitializeComponent();
ViewModel = new MyViewModel();
this.DataContext = this;
}
}
在XAML:
<TextBlock Text="{Binding ViewModel.Title}" />
解决方案是使用新的 属性 以避免转换和嵌套 class 访问 XAML 文件。所以解决方案是使用类似的东西:
public sealed partial class MainPage : BasePage
{
public sealed class MyViewModel : BaseViewModel
{
public string Title
{
get
{
return "Test";
}
}
}
public MyViewModel LocalViewModel
{
get
{
return (MyViewModel) ViewModel;
}
}
public MainPage()
{
this.InitializeComponent();
ViewModel = new MyViewModel();
}
}
因此,使用 x:Bind
语法,XAML 看起来像:
<TextBlock Text="{x:Bind LocalViewModel.Title}" />