访问另一个页面中的图像控件,类Windows手机

时间:2015-12-10 12:42:35

标签: c# xaml windows-phone

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

enter image description here

1 个答案:

答案 0 :(得分:1)

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

例如:
假设您从class A导航到class Bclass 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个bool,每个bool代表一个图像的状态:

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 A&#39; OnNavigatedTo中,执行:

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;
                    }
                }
            }
        }
    }
}

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

希望它有所帮助! :)