【问题标题】:Access a image control in another page, class windows phone访问另一个页面中的图像控件,类 windows phone
【发布时间】:2016-03-16 02:33:02
【问题描述】:

可以从C#XAML 中的另一个类访问control (image) 吗? 例如:在 A 类(图像)中折叠/隐藏,当检查图像是否在 B 类中折叠/隐藏时,我想要可见/启用,这可能吗? 谢谢!

【问题讨论】:

    标签: c# xaml windows-phone


    【解决方案1】:

    您可以使用PhoneApplicationService 来执行此操作。

    例如:
    假设您从 class A 导航到 class B
    class B 中,在导航回 A 类之前,设置

    PhoneApplicationService.Current.State["showImage"] = true;
    

    class A中,实现OnNavigatedTo处理:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (PhoneApplicationService.Current.State.ContainsKey("showImage"))
        {
            bool showImage = (bool)PhoneApplicationService.Current.State["showImage"];
            if (showImage)
            {
                this.YourImage.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                this.YourImage.Visibility = System.Windows.Visibility.Collapsed;
            }
    
            PhoneApplicationService.Current.State.Remove("showImage");
        }
    }
    

    编辑:

    对于多张图片,可以尝试以下方法:

    class B 中,不是将bool 传递给PhoneApplicationService,而是传递一个Dictionary 的布尔值,每个布尔值代表图像的状态:

    var showImage = new Dictionary<int, bool>();
    showImage[1] = true;
    showImage[2] = false;
    showImage[3] = true;
    PhoneApplicationService.Current.State["showImage"] = showImage;
    

    class A 中,为您的图像创建字典:

    private Dictionary<int, Image> _images = new Dictionary<int, Image>();
    

    然后在其构造函数中,用您的图像填充字典:

    InitializeComponent();
    
    _images[1] = YourImage1;
    _images[2] = YourImage2;
    _images[3] = YourImage3;
    

    class AOnNavigatedTo 中,执行:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (PhoneApplicationService.Current.State.ContainsKey("showImage"))
        {
            var showImage = PhoneApplicationService.Current.State["showImage"] as Dictionary<int, bool>;
            if (showImage != null)
            {
                foreach (var key in showImage.Keys)
                {
                    if (_images.ContainsKey(key))
                    {
                        if (showImage[key])
                        {
                            _images[key].Visibility = System.Windows.Visibility.Visible;
                        }
                        else
                        {
                            _images[key].Visibility = System.Windows.Visibility.Collapsed;
                        }
                    }
                }
            }
        }
    }
    

    如果您愿意,可以更改字典的 key 以获得更具代表性的字符串。

    希望对您有所帮助! :)

    【讨论】:

    • 非常感谢,工作几乎完美,因为例如,如果我们有一个包含多个对象的列表,并且只想为那些包含某些值的对象显示图像: Object 1 2 3 4 5 6 Show image for object 2 4 6 一个解决方案,我尝试了每个包含 x 的指令,图像将可见。
    • 有什么更新吗?仅在标记某些项目时显示图像
    • 感谢您的帮助,但是在 B 类中,我对我的物品进行了一些检查,如果某个值等于某个条件,则使我的图像可见。我放了一个截图很好的解释了我想要做什么。
    • 是否有应用程序已经这样做了?只是为了让我更好地了解您的情况。 :)
    • 是的,在 android 上存在一个应用程序,但在 windows 手机中不起作用,如果我有两个项目错误需要显示两个警告图片,请参见示例。
    猜你喜欢
    • 1970-01-01
    • 2018-12-16
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多