如何从另一个线程设置窗口所有者

时间:2014-10-07 04:35:40

标签: c# wpf multithreading

来自MSDN库,在本主题中:“多个Windows,多个线程”,这是给出的代码:

<Window x:Class="SDKSamples.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MultiBrowse"
Height="600" 
Width="800"
Loaded="OnLoaded"
>
<StackPanel Name="Stack" Orientation="Vertical">
<StackPanel Orientation="Horizontal">
  <Button Content="New Window"
          Click="NewWindowHandler" />
  <TextBox Name="newLocation"
           Width="500" />
  <Button Content="GO!"
          Click="Browse" />
</StackPanel>
<Frame Name="placeHolder"
        Width="800"
        Height="550"></Frame>
</StackPanel>
</Window>

代码背后:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Threading;
using System.Threading;


namespace SDKSamples
{
public partial class Window1 : Window
{

    public Window1() : base()
    {
        InitializeComponent();
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
       placeHolder.Source = new Uri("http://www.msn.com");
    }

    private void Browse(object sender, RoutedEventArgs e)
    {
        placeHolder.Source = new Uri(newLocation.Text);
    }

    private void NewWindowHandler(object sender, RoutedEventArgs e)
    {       
        Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint));
        newWindowThread.SetApartmentState(ApartmentState.STA);
        newWindowThread.IsBackground = true;
        newWindowThread.Start();
    }
    private void ThreadStartingPoint()
    {
        Window1 tempWindow = new Window1();
        tempWindow.Show();       
        System.Windows.Threading.Dispatcher.Run();
    }
}
}

如何将tempWindow的所有者设置为主窗口?我的应用程序中有多个窗口需要同时更新,我想使用它。

0 个答案:

没有答案