从 cs 获取变量并在 xaml (uwp) 中使用它

Take variable from cs and use it in xaml (uwp)

如何在 XAML 文件中使用 C# 字符串的值?

这是我的部分代码: CS 文件:

public sealed partial class SomePage : Page
    {

        public SomePage ()
        {
            AppVersion = "some text" + XDocument.Load ("WMAppManifest.xml").Root.Element ("App").Attribute ("Version").Value.ToString();
            this.InitializeComponent ();

        }

        public string AppVersion{get; set;}

XAML 文件:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
     EntranceNavigationTransitionInfo.IsTargetElement="True">
        <StackPanel>
             <TextBlock x:Name="VersionTextBlock" Text="{Binding ElementName=VersionTextBlock, FallbackValue= AppVersion}"/>

您的绑定引用了 TextBlock,这不是您的 AppVersion 所在的位置。由于 AppversionPage 上的 属性,您可以使用 compile-time 绑定,如下所示:

<TextBlock Text="{x:Bind AppVersion}"/>

另一方面,动态绑定是相对于 PageDataContext 而言的,这意味着如果您想这样做:

<TextBlock Text="{Binding AppVersion}"/>

...您必须将 Pages DataContext 设置为具有名为 AppVersion 的 属性 的某个对象,例如,

public SomePage ()
{
    AppVersion = "some text" + ...;
    InitializeComponent ();
    DataContext = this; // More common is to have a separate viewmodel class, though.
}