【问题标题】:Button Screen Change in Android Mono using delegates使用委托在 Android Mono 中更改按钮屏幕
【发布时间】:2012-03-28 22:05:19
【问题描述】:

以下代码来自我的 Android Mono 应用程序的 C# 部分。它最终将成为万用表模拟器的 GUI,但现在只显示文本。这是相当直截了当的:

-单击其中一个按钮以转到该表(电压表、电流表、欧姆表) -单击“重新扫描”按钮,TextView 会告诉您单击该按钮的次数。 - 单击其他仪表按钮之一或主页按钮以切换视图

这么多工作完美无缺。不幸的是,一旦我切换视图,按钮就会停止工作。下面是 Ohm 按钮和 Amp 按钮的代码。 Ohm 按钮是“完整”按钮,可显示所有其他屏幕的视图。出于测试目的,我要去放大器屏幕,但是当我去那里时,它的重新扫描按钮什么也没做。没有一个按钮做任何事情。

相当确定问题在于我对 delegate 命令的使用,但我的研究都没有以任何方式引导我找到解决方案。

如果需要,我可以提供更多的主要代码和 XML 代码。

ampButton.Click += delegate
            {
                SetContentView(Resource.Layout.AmpScreen);
                Button ampButtonData = FindViewById<Button>(Resource.Id.CurrentButtonamp);
                TextView ampData = FindViewById<TextView>(Resource.Id.ampdata);
                ampButtonData.Click += delegate
                {
                    ampData.Text = string.Format("{0} clicks!", count2++);
                };
                Button amp2volt = FindViewById<Button>(Resource.Id.Amp2VoltButton);
                Button amp2ohm = FindViewById<Button>(Resource.Id.Amp2OhmButton);
                Button amp2home = FindViewById<Button>(Resource.Id.Amp2HomeButton);
            };


            ohmButton.Click += delegate
            {
                SetContentView(Resource.Layout.OhmScreen);
                Button ohmButtonData = FindViewById<Button>(Resource.Id.CurrentButtonohm);
                TextView ohmData = FindViewById<TextView>(Resource.Id.ohmdata);
                ohmButtonData.Click += delegate
                {
                    ohmData.Text = string.Format("{0} clicks!", count3++);
                };

                Button ohm2amp = FindViewById<Button>(Resource.Id.Ohm2AmpButton);
                Button ohm2volt = FindViewById<Button>(Resource.Id.Ohm2VoltButton);
                Button ohm2home = FindViewById<Button>(Resource.Id.Ohm2HomeButton);

                ohm2amp.Click += delegate
                {
                    SetContentView(Resource.Layout.AmpScreen);
                };

                ohm2volt.Click += delegate
                {
                    SetContentView(Resource.Layout.VoltScreen);
                };

                ohm2home.Click += delegate
                {
                    SetContentView(Resource.Layout.Main);
                };

            };

【问题讨论】:

    标签: c# android visual-studio-2010 mono xamarin.android


    【解决方案1】:

    我认为你的问题是你每次都替换整个视图 - 所以按钮实例正在改变。

    SetContentView 内部发生的情况是,InflatorService 被要求根据传入的 XML 创建一组全新的 UI 对象,现有 UI 被清除干净,然后将这些新 UI 对象放置在它们的位置。

    新的 UI 对象是否碰巧与旧对象具有相同的资源标识符并不重要 - 它们仍然是单独的实例。

    如果您想继续使用当前方法,则需要在每个 SetContentView 之后重新连接所有事件 - 例如

            ohm2amp.Click += delegate
                {
                    SetContentView(Resource.Layout.AmpScreen);
                    RewireEvents();
                };
    

           private void RewireEvents()
           {
               var ohm2home = FindViewById<Button>(Resource.Id.ohm2home);
               ohm2home.Click += { /* todo */ };
               // etc
           }
    

    或者,可以考虑使用不同的 UI:

    • 例如您可以更改不同子布局的可见性,而不是调用 SetContentView 来替换所有内容
    • 例如或者您可以使用多个活动(或选项卡)而不是单个活动

    希望有帮助

    【讨论】:

    • 非常感谢斯图尔特。我进行了您建议的更改并实施了一些初步测试,并对他们的成功感到高兴。就完成整体结构而言,我的整个 GUI 现在已经完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多