从另一个窗口更改页面上的标签 C# wpf

发布于 2024-11-10 05:02:45 字数 745 浏览 1 评论 0原文

我已经制作了一个带有导航窗口的应用程序,其中加载了一个页面。这也是该项目中的另一个(正常)窗口。 这些窗口/页面的名称:

NavigationWindows: MainWindows
Page: Page1
Window: TwitterConnect
Label on Page: label4

我的 Page1 上有一个标签,我想从 TwitterConnect 更改它。 因为我创建了 Page1 的新实例来调用方法 ConnectToTwitter(),所以页面上的标签不会更新。 这是 TwitterConnect 的 de codebehind 中的代码:

        private void button1_Click(object sender, RoutedEventArgs e)
        {  
            string pin = twitpin.Text;    

            Page1 page = new Page1();

            page.ConnectToTwitter(pin, genratedToken);

            this.Close();     
        }

我搜索谷歌以找到解决方案,但我不明白。 我认为这与调度员有关?!

我真的是 C#、VS10express 和 WPF 的初学者。

如何从 TwitterConnect 更改 label4? 您能用一段代码解释一下吗?

I have made an application with a NavigationWindow with a page loaded in it. Also is another (normal) window in this project.
The names of these windows/pages:

NavigationWindows: MainWindows
Page: Page1
Window: TwitterConnect
Label on Page: label4

I have a label on my Page1 and I want to change it from TwitterConnect.
Because I make a new instance of Page1 to call method ConnectToTwitter(), the label on my page doesn't update.
Here is the code in de codebehind of TwitterConnect:

        private void button1_Click(object sender, RoutedEventArgs e)
        {  
            string pin = twitpin.Text;    

            Page1 page = new Page1();

            page.ConnectToTwitter(pin, genratedToken);

            this.Close();     
        }

I searched google to find a solution, but I don't get it.
I think it has something to do with Dispatcher?!

I'm really a beginner in C#, VS10express and WPF.

How can I change label4 from TwitterConnect?
Can you please explain it with a snippet of code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你是孤单的心事 2024-11-17 05:02:45

您正在从 TwitterConnect 窗口创建 Page1.xaml 的新实例。您需要做的是找到一种方法来访问 Page1.xaml 的当前实例,这可以通过使用 WindowDataContext 属性轻松实现em> 类。

Page1.xaml.cs

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    TwitterConnect twitterWindow = new TwitterConnect();
    // this gives TwitterConnect access to Page1's label4 property
    twitterWindow.DataContext = this;
    twitterWindow.Show();
}

TwitterConnect.xaml.cs

private void ButtonClick(object sender, RoutedEventArgs e)
{
    Page1 page1 = (Page1)this.DataContext;

    string pin = twitpin.Text;    
    page1.ConnectToTwitter(pin, genratedToken);
    // Then you can update the label like so:
    page1.Label4.Text = "The text you want to display on the label";
    this.Close();   
}

如果我错过了任何内容,请告诉我。另外,我强烈建议不要使用这种配置,因为它可能会成为维护的噩梦。更好的方法可能是在 Page1.cs 上添加方法或属性来处理 label4 上的文本设置。

You are creating a new instance of Page1.xaml from the TwitterConnect window. What you need to do is find a way to access the current instance of Page1.xaml which can easily be acheived by using the DataContext property of the Window class.

Page1.xaml.cs

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    TwitterConnect twitterWindow = new TwitterConnect();
    // this gives TwitterConnect access to Page1's label4 property
    twitterWindow.DataContext = this;
    twitterWindow.Show();
}

TwitterConnect.xaml.cs

private void ButtonClick(object sender, RoutedEventArgs e)
{
    Page1 page1 = (Page1)this.DataContext;

    string pin = twitpin.Text;    
    page1.ConnectToTwitter(pin, genratedToken);
    // Then you can update the label like so:
    page1.Label4.Text = "The text you want to display on the label";
    this.Close();   
}

If I've missed anything out please let me know. Also, I strongly advise against this configuration as it could become a nightmare to maintain. A better approach might be to add a method or property onto Page1.cs to handle setting the text on label4.

丿*梦醉红颜 2024-11-17 05:02:45

如果我正确理解你的问题,你需要刷新控件。

这是一个新课程

public static class ExtensionMethods
{
 private static Action EmptyDelegate = delegate() { };

 public static void Refresh(this UIElement uiElement)
 {
  uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
 }
}

这是您班级中的示例方法
(第 1 页?)

private void LoopingMethod()
{
for (int i = 0; i < 10; i++)
{
  label1.Content = i.ToString();
  label1.Refresh();
  Thread.Sleep(500);
}
}

请检查此链接

If I have understood your question correctly, you need to refresh the control.

this is a new class

public static class ExtensionMethods
{
 private static Action EmptyDelegate = delegate() { };

 public static void Refresh(this UIElement uiElement)
 {
  uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
 }
}

this is example method in your class
(page1?)

private void LoopingMethod()
{
for (int i = 0; i < 10; i++)
{
  label1.Content = i.ToString();
  label1.Refresh();
  Thread.Sleep(500);
}
}

please check this link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文