编辑2:为了澄清我想要做的事情,我创建了一个小样本项目。这是MainWindow。
<Grid>
<StackPanel Orientation="Vertical" >
<Grid Background="#FF937BFF">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Button Name="Show_Page1"
Content="Page1"
Grid.Column="0"
Margin="10 5" Click="Show_Page1_Click">
</Button>
<Button Name="Show_Page2"
Content="Page2"
Grid.ColumnSpan="1"
Margin="10 5"
Grid.Column="1" Click="Show_Page2_Click">
</Button>
</Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2.5*"/>
<ColumnDefinition Width="0.5*"/>
</Grid.ColumnDefinitions>
<Frame Height="290"
Name="MainFrame"
Grid.Column="0"
NavigationUIVisibility="Hidden"/>
<Frame Height="290"
Name="SecondFrame"
Grid.Column="1"
NavigationUIVisibility="Hidden"/>
</Grid>
</StackPanel>
</Grid>
和c#代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MainFrame.Content = new Page1();
SecondFrame.Content = new Page_Other();
}
private void Show_Page1_Click(object sender, RoutedEventArgs e)
{
MainFrame.Content = new Page1();
}
private void Show_Page2_Click(object sender, RoutedEventArgs e)
{
MainFrame.Content = new Page2();
}
}
两者,Page1和Page2具有相同的外观。它只是一个更改TextBlock内容的按钮。这是我的问题。 TextBlock本身位于另一个名为Page_Other的页面上,而触发此操作的按钮位于Page1和Page2上。
因为我不希望这篇文章在最后变得更长,所以只是以Page1.cs为例:
public Page1()
{
InitializeComponent();
}
private void BTN_Change_TxtBlock_Click(object sender, RoutedEventArgs e)
{
// Access function to - or directly change text inside TextBlock "Name:Txt_To_Change" on "Page_Other"
}
答案 0 :(得分:0)
If you want multiple classes to talk you will need another class that manages this behavior. (I mean you could tightly couple them but that is bad for so many reasons.)
What many programs do is implement a queue. You create a class that is static or a Singleton pattern so every class can access it. Then you make that class an Observer pattern so classes can subscribe and unsub from various events. This way you can publish an event from a class without knowing exactly what will pick up that event and run with it. This allows you flexibility as you can easily swap classes in and out without causing an issue.
Here is one example of a MessageQueue as a Singleton:
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using log4net;
namespace MyApp
{
public interface IMessageQueue
{
void Subscribe(string topic, IMessageSubscriber subscriber);
void UnSubscribe(string topic, IMessageSubscriber subscriber);
void Notify(string topic, Dictionary<string, string> fields);
bool AnyoneInterested(string topic);
}
public class MessageQueue : IMessageQueue
{
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private readonly Dictionary<string, List<IMessageSubscriber>> subscriptions =
new Dictionary<string, List<IMessageSubscriber>>();
private static MessageQueue instance;
private MessageQueue()
{
}
public static MessageQueue GetInstance()
{
return instance ?? (instance = new MessageQueue());
}
public void Subscribe(string topic, IMessageSubscriber subscriber)
{
lock (subscriptions)
{
if (subscriptions.ContainsKey(topic))
{
subscriptions[topic].Add(subscriber);
}
else
{
subscriptions.Add(topic, new List<IMessageSubscriber> {subscriber});
}
}
}
public void UnSubscribe(string topic, IMessageSubscriber subscriber)
{
lock (subscriptions)
{
if (subscriptions != null && !subscriptions.ContainsKey(topic)) return;
subscriptions[topic].Remove(subscriber);
}
}
public void Notify(string topic, Dictionary<string, string> fields)
{
lock (subscriptions)
{
if (!subscriptions.ContainsKey(topic)) return;
log.Info($"Message: {topic}");
foreach (var sub in subscriptions[topic])
{
Task.Run(() =>
{
try
{
sub.Notify(topic, fields);
}
catch
{
//supress, should only get here if someone disposed a subscriber without calling unsubscribe
}
});
}
}
}
public bool AnyoneInterested(string topic)
{
lock (subscriptions)
{
return subscriptions.ContainsKey(topic);
}
}
}
}
And the IMessageSubscriber:
using System.Collections.Generic;
namespace MyApp
{
public interface IMessageSubscriber
{
void Notify(string topic, Dictionary<string, string> fields);
}
}