0
我有WPF控制託管在winform中有菜單和一些標籤。 WPF控件連接到互聯網以下載一些數據。 我把代碼分成兩步 第一隻是設置控制屬性 第二連接到網 第二次運行在線程內, 但Winform控件不會發生在窗體上,直到WPF控件完成他的兩個步驟。WPF控制託管在WinForm塊WinForm控件從加載甚至與線程
我已經嘗試了很多方法使它可以穿線 但是所有的方法都會到達相同的目的地。
CODE 1-負載WPF控件
private void MDI_Load(object sender, EventArgs e)
{
MenuManager.FillMenu(MainMenu); // I have filled WinForm Menu first, but it doesn't appear until WPF finish
#region = WPF Control =
wpfManager.AddweatherControl();
wpfManager.weatherManager.Start(); // This have to run in another thread
#endregion
}
2- wpfManager.weatherManager.Start
public void Start()
{
//var tsk = System.Threading.Tasks.Task.Factory.StartNew(GetWeather);
//tsk.ContinueWith(t => { MessageBox.Show(t.Exception.InnerException.Message); },
// System.Threading.CancellationToken.None, System.Threading.Tasks.TaskContinuationOptions.OnlyOnFaulted,
// System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext());
//System.Threading.Thread t = new System.Threading.Thread(
// () => weatherControl.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(GetWeather))
// );
//t.SetApartmentState(System.Threading.ApartmentState.STA);
//t.Start();
weatherControl.Dispatcher.BeginInvoke(new Action(GetWeather), new object[] { });
}
void GetWeather()
{
#region = Weather =
Yweather.Getweather(UserManager.CurrentUser.Preferences.WeatherCity);
if (Yweather.Online && Yweather.IDayForecast.Count > 0)
{
weatherControl.CurrentDegree.Text = Yweather.IDayForecast[0].CurrentTemperature.ToString();
weatherControl.WeatherTypeName.Text = Yweather.IDayForecast[1].WeatherText;
weatherControl.AllDayDegree.Text = Yweather.IDayForecast[1].LowTemperature + " - " + Yweather.IDayForecast[1].HighTemperature;
weatherControl.WeatherType.Source = wpfManager.BitmapToImageSource(Yweather.IDayForecast[0].Image);
xWeatherDay weatherday1 = weatherControl.OhterDaysPanel.Children[0] as xWeatherDay;
weatherday1.AllDayDegree.Text = Yweather.IDayForecast[2].LowTemperature + " - " + Yweather.IDayForecast[2].HighTemperature;
weatherday1.WeatherType.Source = wpfManager.BitmapToImageSource(Yweather.IDayForecast[2].Image);
}
else
{
weatherControl.CurrentDegree.Text = "0";
weatherControl.WeatherTypeName.Text = "NAN";
weatherControl.AllDayDegree.Text = "0 - 0";
weatherControl.WeatherType.Source = wpfManager.BitmapToImageSource(Yweather.OfflineImage);
}
#endregion
}
我不知道該如何謝謝你,它的功能就像一個魅力。 :) –