我对 C# 和 WPF 编程还很陌生,现在这个问题让我很头疼。
程序应该做什么:程序显示一个带有文本框的欢迎屏幕,客户可以在其中输入他的名字。如果设备靠近无线 LAN 接入点并成功连接,则此主窗口上的“开始”按钮应可见。
什么不起作用:在事件处理程序中设置按钮可见性。未应用新样式。此外,如果我在设置可见性属性/属性(?)之后尝试调用任何其他代码,它将不会运行(如果我在设置 visib 之前放置了 MessageBox.Show。如果我在设置后放置它,它将显示该属性,甚至不会再访问此代码)。
编码:
这是按钮元素:
<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" />
这是 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();
IConnectionPointContainer icpc = (IConnectionPointContainer)nlm_;
Guid tempGuide = typeof(INetworkListManagerEvents).GUID;
icpc.FindConnectionPoint(ref tempGuide, out nlmICP_);
nlmICP_.Advise(this, out nlmCookie_);
}
最后,事件处理程序:
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;
}
}
就是这样——我希望你能帮助我。
非常感谢您的努力!