【问题标题】:How to update item xaml from another class in UWP如何从 UWP 中的另一个类更新项目 xaml
【发布时间】:2022-01-25 11:58:28
【问题描述】:

大家好我是UWP开发的新手,我在网上搜索了很多但我没有找到实现目标的正确方法,我想做的是通过使用更新我的MainPage的ui属于 UiUpdate 类的类。这是我想要的,这是我的主页:

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

这是与我的 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>

现在通过 UiUpdate 类,我想更新我的 TextBlocks 或我的 UI 的任何其他元素,我在网上找到了这样的东西:

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

             });

但我无法在 UiUpdate 类中以任何方式访问 MainPage 的 texbox 元素,我无法执行 MainPage.text_one ..,我也无法找到有关要应用的模式或其他方面的良好文档用于实现我的 UWP 应用

【问题讨论】:

    标签: c# uwp uwp-xaml


    【解决方案1】:

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

    为了解释这个问题,你可能需要参考UWP mvvm design,你提到UiUpdate最喜欢ViewModel。

    例如

    <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>
    

    背后的代码

    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();
            }
        }
    
    }
    

    【讨论】:

    • 什么是“ViewModel”类,我无法使用 ViewModel.TextBlcokText 在我的 MainPage 中访问 _textBlcokText
    • 我在 xaml 中定义了 ViewModel 像这样&lt;local:UiUpdate x:Name="ViewModel" /&gt;,它不是类名,它是 UiUpdate 的实例
    • 它的工作 tankiu
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多