将 WPF 按钮设置为在 IConnectionPoint 事件处理程序中可见

发布于 2024-10-25 05:55:31 字数 2164 浏览 1 评论 0原文

我对 C# 和 WPF 编程相当陌生,现在这个问题让我头疼。

程序应执行的操作:程序会显示一个带有文本框的欢迎屏幕,客户可以在其中输入自己的姓名。 如果设备靠近无线 LAN 接入点并成功连接,则该主窗口上的“开始”按钮应变为可见。

什么不起作用:在事件处理程序内设置按钮可见性。新样式不会被应用。此外,如果我在设置可见性属性/属性(?)后尝试调用任何其他代码,它将不会运行(如果我在设置可见性之前放置 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;
        }
    }

就是这样 - 我希望您能帮助我。

预先非常感谢您的努力!

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();
        IConnectionPointContainer 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!

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

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

发布评论

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

评论(2

榆西 2024-11-01 05:55:31

嗯 - 现在我对回答我自己的问题感到难过,但是我解决这个问题的冲动让我一直在谷歌上搜索,最后我发现,我只能从 UI 线程更改我的 WPF 元素,但是 INetwork-Events 是在不同的线程。

所以我这样解决了:

    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;
                }
            ));
        }
        // ...
    }

不过谢谢你的时间:)

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 :)

瑾夏年华 2024-11-01 05:55:31

不是对您的问题的直接答案(因为它已经得到解答),但 WPF 的处理方法是拥有一个将可见性绑定到的属性 CanConnect (使用 bool 到可见性转换器)。

每当您触摸代码中的 UI 控件时,就很好地表明您的设计需要改进。

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.

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