访问另一个页面中的图像控件,class windows phone
Access a image control in another page, class windows phone
可以从 C#
、XAML
中的另一个 class 访问 control (image)
吗?
例如:In class A (image) is collapsed/hidden, check if image is collapsed/hidden in class B, i want to be visible/enabled, 有可能?
谢谢!
您可以使用 PhoneApplicationService
来完成。
例如:
假设您从 class A
导航到 class B
。
在 class B
中,在导航回 class 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 A
的 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;
}
}
}
}
}
}
如果您愿意,可以更改字典的 key 以获得更具代表性的字符串。
希望对您有所帮助! :)
可以从 C#
、XAML
中的另一个 class 访问 control (image)
吗?
例如:In class A (image) is collapsed/hidden, check if image is collapsed/hidden in class B, i want to be visible/enabled, 有可能?
谢谢!
您可以使用 PhoneApplicationService
来完成。
例如:
假设您从 class A
导航到 class B
。
在 class B
中,在导航回 class 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 A
的 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;
}
}
}
}
}
}
如果您愿意,可以更改字典的 key 以获得更具代表性的字符串。
希望对您有所帮助! :)