简体   繁体   English

如何从 UWP 中的另一个 class 更新项目 xaml

[英]How to update item xaml from another class in UWP

Hi everyone i am a newbie in UWP development, i have searched a lot on the net but I have not found the right way to achieve my goal, what i would like to do is update the ui of my MainPage through the use of a class belonging to UiUpdate class.大家好,我是 UWP 开发的新手,我在网上搜索了很多但我没有找到实现目标的正确方法,我想做的是通过使用 class 更新我的 MainPage 的 ui属于 UiUpdate class。 Here's what I'd like to get, this is my MainPage:这是我想要得到的,这是我的主页:

namespace Test_App
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
    }
}

This is the relative xaml associated with my MainPage:这是与我的 MainPage 关联的相对 xaml:

<Page
    x:Class="Test_App.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Test_App"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid>
        <controls:DockPanel>
            <controls:DockPanel Visibility="Visible"  HorizontalAlignment="Left" >
                <Grid>
                    <TextBlock x:Name="text_one"  HorizontalAlignment="Right" Margin="0,50,46,0" VerticalAlignment="Center" FontWeight="Bold" />
                    <TextBlock  x:Name="text_two" HorizontalAlignment="Left" VerticalAlignment="Bottom" FontWeight="Medium" />

                </Grid>
            </controls:DockPanel>
    </Grid>
</Page>

Now through the UiUpdate class, I would like to update my TextBlocks or any other element of my UI, I found something like this on the net:现在通过 UiUpdate class,我想更新我的 TextBlocks 或我的 UI 的任何其他元素,我在网上找到了这样的东西:

await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync (CoreDispatcherPriority.Normal, () =>
             {
                 // Update texboxt

             });

But i can't access in any way in the UiUpdate class the texbox elements of the MainPage i can't do MainPage.text_one.., also i can't find good documentation for what concerns patterns to be applied or other for the realization of my UWP app但是我无法在 UiUpdate class 中以任何方式访问 MainPage 的 texbox 元素我不能做 MainPage.text_one ..,我也找不到关于要应用的模式或其他实现的好的文档我的 UWP 应用程序

How to update item xaml from another class in UWP如何从 UWP 中的另一个 class 更新项目 xaml

For explain this question, you may need to refer UWP mvvm design , and you mentioned UiUpdate most like ViewModel.为了解释这个问题,你可能需要参考UWP mvvm design ,你提到UiUpdate最像 ViewModel。

For example例如

<Page.DataContext>
    <local:UiUpdate x:Name="ViewModel" />
</Page.DataContext>
<Grid>
    <TextBlock
        x:Name="text_one"
        Margin="0,50,46,0"
        HorizontalAlignment="Right"
        VerticalAlignment="Center"
        FontWeight="Bold"
        Text="{Binding TextBlockText}" />
    <Button VerticalAlignment="Bottom" Click="Button_Click">Update</Button>
</Grid>

Code behind背后的代码

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }
    private Random random = new Random();
    private void Button_Click(object sender, RoutedEventArgs e)
    {         
        ViewModel.TextBlockText = $"Text-----{random.Next(15)}";
    }
}

public class UiUpdate : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    }
    private string _textBlcokText;
    public string TextBlockText
    {
        get
        {
            return _textBlcokText;
        }
        set
        {
            _textBlcokText = value;
            OnPropertyChanged();
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM