I'm fairly new to C# and WPF programing and right now this problem is giving me headaches.
What the program should do: The program displays a welcome screen with a text box, where a customer can enter his name. If the device comes near a Wireless Lan Access Points and connects successfully, a "Start" button on this main window should become visible.
What's not working: Setting the button visibility inside the event handler. The new style does not get applied. Furthermore, if I try to call any other code after setting the visibility attribute/property(?) it won't run (if I put a MessageBox.Show right before setting the visib. it will be shown, if I put it after setting the property, this code won't even be reached any more).
The code:
This is the button Element:
<Button Height="72" HorizontalAlignment="Center" Margin="319,507,315,0"
Name="buttonStart" VerticalAlignment="Top" Width="168" FontSize="32"
Content="Los geht's" Click="buttonStart_Click" />
This is the event handler registration within the MainWindow.xaml.cs
public partial class MainWindow : Window, INetworkListManagerEvents
{
private INetworkListManager nlm_;
private IConnectionPoint nlmICP_;
private int nlmCookie_ = 0;
private void InitNetManager()
{
nlm_ = new NetworkListManager();
IConnectionPointContai开发者_运维百科ner icpc = (IConnectionPointContainer)nlm_;
Guid tempGuide = typeof(INetworkListManagerEvents).GUID;
icpc.FindConnectionPoint(ref tempGuide, out nlmICP_);
nlmICP_.Advise(this, out nlmCookie_);
}
And finally, the event handler:
public void ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
{
if (newConnectivity == NLM_CONNECTIVITY.NLM_CONNECTIVITY_DISCONNECTED ||
((int)newConnectivity & (int)NLM_CONNECTIVITY.NLM_CONNECTIVITY_IPV4_NOTRAFFIC) != 0)
{
MessageBox.Show("Disconnected"); // this will code is reached
buttonStart.Visibility = Visibility.Hidden; // this is not getting applied
MessageBox.Show("Disconnected"); // this will code is not reached (stepped with debugger)
}
if ((((int)newConnectivity & (int)NLM_CONNECTIVITY.NLM_CONNECTIVITY_IPV4_LOCALNETWORK) != 0) ||
(((int)newConnectivity & (int)NLM_CONNECTIVITY.NLM_CONNECTIVITY_IPV4_INTERNET) != 0))
{
MessageBox.Show("Connected"); // see comments above
buttonStart.Visibility = Visibility.Visible;
}
}
That's it - I hope you can help me.
Thank you very much in advance for your efforts!
Hum - now I feel bad for answering my own question, but my urge to solve this problem kept me googling and finally I found out, that I can only change my WPF elements from the UI thread, but the INetwork-Events are called within a different thread.
So I solved it this way:
public void ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
{
if (newConnectivity == NLM_CONNECTIVITY.NLM_CONNECTIVITY_DISCONNECTED ||
((int)newConnectivity & (int)NLM_CONNECTIVITY.NLM_CONNECTIVITY_IPV4_NOTRAFFIC) != 0)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(
delegate()
{
buttonStart.Visibility = Visibility.Hidden;
}
));
}
// ...
}
Thanks for your time though :)
Not a direct answer to your question (because it's already answered) but the WPF way to do things would be to to have a property CanConnect you bind the visibility to (with the bool to visibility converter).
Whenever you touch a UI control in your code then it's a very good indicator that your design needs improvement.
精彩评论