我正在開發具有多個框架和頁面的 WinUI 應用程式(c# 和 xaml)。問題是我需要修改另一個類的 UIElement 屬性(TextBox.Text)。我一直在嘗試很多東西,但都沒有奏效。如果有人能用一些有用的方法來激勵我,我會很高興。它可以是 xaml 資料系結 (<property={"Binding bindingName"}) 之外的任何內容。
謝謝您的幫助。
uj5u.com熱心網友回復:
在這個示例代碼中,我有 2 個頁面(Page1和Page2)和 1 個 ViewModel(MainViewModel)。每個頁面都有一個TextBox系結到MainViewModel.
如果更改 中的文本Page1,Page2則將更改 中的文本,反之亦然。
NuGet 包
- 社區工具包.Mvvm
- Microsoft.Extensions.DependencyInjection
應用程式.xaml.cs
using CommunityToolkit.Mvvm.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
namespace MultiplePagesSingleViewModel;
public partial class App : Application
{
private Window? window;
public App()
{
this.InitializeComponent();
Ioc.Default.ConfigureServices(
new ServiceCollection()
// This needs to be Singleton
.AddSingleton<MainViewModel>()
.BuildServiceProvider());
}
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
this.window = new MainWindow();
this.window.Activate();
}
}
Page1.xaml
<Page
x:Class="MultiplePagesSingleViewModel.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:MultiplePagesSingleViewModel"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<StackPanel>
<TextBlock Text="Page 1" />
<TextBox Text="{x:Bind ViewModel.SomeText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Page>
Page1.xaml.cs
using CommunityToolkit.Mvvm.DependencyInjection;
using Microsoft.UI.Xaml.Controls;
namespace MultiplePagesSingleViewModel;
public sealed partial class Page1 : Page
{
public Page1()
{
this.InitializeComponent();
// This MainViewModel is the same instance that Page2 gets.
ViewModel = Ioc.Default.GetRequiredService<MainViewModel>();
}
public MainViewModel ViewModel { get; }
}
Page2.xaml
<Page
x:Class="MultiplePagesSingleViewModel.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:MultiplePagesSingleViewModel"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<StackPanel>
<TextBlock Text="Page 2" />
<TextBox Text="{x:Bind ViewModel.SomeText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Page>
Page2.xaml.cs
using CommunityToolkit.Mvvm.DependencyInjection;
using Microsoft.UI.Xaml.Controls;
namespace MultiplePagesSingleViewModel;
public sealed partial class Page2 : Page
{
public Page2()
{
InitializeComponent();
// This MainViewModel is the same instance that Page1 gets.
ViewModel = Ioc.Default.GetRequiredService<MainViewModel>();
}
public MainViewModel ViewModel { get; }
}
主視圖模型.cs
using CommunityToolkit.Mvvm.ComponentModel;
namespace MultiplePagesSingleViewModel;
// This class needs to be "partial" for CommunityToolkit.Mvvm.
public partial class MainViewModel : ObservableObject
{
[ObservableProperty]
// The CommunityToolkit.Mvvm will automatically generate
// a UI-Interactive "SomeText" property for you.
private string someText = "Default text";
}
主視窗.xaml
<Window
x:Class="MultiplePagesSingleViewModel.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:MultiplePagesSingleViewModel"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid ColumnDefinitions="*,*">
<local:Page1 Grid.Column="0" />
<local:Page2 Grid.Column="1" />
</Grid>
</Window>
uj5u.com熱心網友回復:
好吧,經過兩周的研究,我終于找到并檢查了一個解決方案。
解決方案基本上是從不同的類中上升和捕捉事件。我在 MSDN 帖子中找到了此資訊。https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-publish-events-that-c??onform-to-net-framework-guidelines
這篇文章非常直截了當。我創建了 CustomEventArgs 類,然后發布者類是我的控制器 (.cs),我的訂閱者是模型 (.xaml.cs)。我遇到的唯一問題是將所有方法和屬性設定為靜態,以便能夠從其他模型呼叫控制器函式。問題是raiseEvent(this, e);在發布者類中,this不能是靜態的,所以我改寫null了。
另外,我想說它運行起來非常流暢,即使我經常使用它也完全沒有延遲或延遲。
我希望這對遇到同樣問題的每個人都有幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/513548.html
上一篇:Android.Util.AndroidRuntimeException'只有創建視圖層次結構的原始執行緒才能接觸其視圖。
