C# 在文本框中显示变量

C# 在文本框中显示变量,c#,xaml,uwp,uwp-xaml,C#,Xaml,Uwp,Uwp Xaml,我很难在文本框中显示变量 主要是试图理解绑定在XAML中的工作方式 我需要尝试在相关字段中显示变量TextBoxFileName和TextBoxFilePath。变量正在检索的信息存储在单独的GlobalVariableStorage类中。我不希望文本框字段可编辑,因此我将它们设置为只读。我不希望用户能够编辑这些字段中的数据。如果您有其他显示方法的想法,请随时提出建议 XAML <Page.Resources> <ResourceDictionary>

我很难在文本框中显示变量

主要是试图理解绑定在XAML中的工作方式

我需要尝试在相关字段中显示变量
TextBoxFileName
TextBoxFilePath
。变量正在检索的信息存储在单独的
GlobalVariableStorage
类中。我不希望
文本框
字段可编辑,因此我将它们设置为只读。我不希望用户能够编辑这些字段中的数据。如果您有其他显示方法的想法,请随时提出建议

XAML

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.ThemeDictionaries>
            <!-- Placeholder for the theme dictionary -->
        </ResourceDictionary.ThemeDictionaries>
    </ResourceDictionary>
</Page.Resources>

<Frame Background="{StaticResource CustomAcrylicDarkBackground}">
    <StackPanel>
        <TextBox Width="500" Header="File Name" PlaceholderText="Name Of File" IsReadOnly="True" Foreground="White" Text=""/>
        <TextBox Width="500" Header="File Location" PlaceholderText="File Location" IsReadOnly="True" Foreground="White" Text=""/>
    </StackPanel>
</Frame>

您正处于困难时期,因为您的方法错误。
例如,您没有遵循MVVM模式。另外,您不需要将
IsReadOnly
设置为true,只需使用单向绑定即可

<TextBox Text="{Binding TextBoxFileName,Mode=OneWay}"/>

为了正确理解和实现MVVM,我建议您阅读以下链接:。虽然它是针对WPF的,但UWP与WPF非常相似,您不会遇到任何问题

如果你想学习MVVM,我可以帮你。只要给我发一条关于“不和谐”的信息:红卫2396


玩得开心。

将两个只读属性添加到
设置页面
类:

public sealed partial class SettingsPage : Page
{
    public SettingsPage()
    {
        this.InitializeComponent();
    }

    public string TextBoxFileName => GlobalVariables.FileName;
    public string TextBoxFilePath => GlobalVariables.FilePath;
}
…并与以下内容相结合:

<TextBox Header="File Name" ... Text="{x:Bind TextBoxFileName}"/>
<TextBox Header="File Name" ... Text="{x:Bind TextBoxFilePath}"/>

太棒了,谢谢!我一定接受你的提议。
<TextBox Header="File Name" ... Text="{x:Bind TextBoxFileName}"/>
<TextBox Header="File Name" ... Text="{x:Bind TextBoxFilePath}"/>