积极答复者
如何在代码中实现对一个Button的图标变化显示进行切换?

问题
-
我想请教下:在xmal设计器中设置button style是通过 <Button 下:Name=“btn” Style=“{StaticResource PhotoAppBarButtonStyle}”/>实现
那么在执行代码时,动态切换呢?
一个button,有两种状态,一种是截图(camera),一种是录像(video),在产生单击响应后,通过一个bool状态变量isVideo控制这两种状态的切换,
想要在界面上显示不同的style,比如自带StaticResource的VideoAppBarButtonStyle和PhotoAppBarButtonStyle。
在单击响应函数
void btn_click() { ... if(isVideo) btn-Style = ??? else btn->Style = ??? }
这里面怎么实现对StaticResource的调用 ,谢谢!
- 已编辑 denny_zhuyi 2012年7月25日 5:05
答案
-
这样即可:
void App13::MainPage::btn1_Click_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) { if(isVideo) safe_cast<Button^>(sender)->Style = safe_cast<Windows::UI::Xaml::Style^>(App::Current->Resources->Lookup(L"VideoAppBarButtonStyle")); else safe_cast<Button^>(sender)->Style = safe_cast<Windows::UI::Xaml::Style^>(App::Current->Resources->Lookup(L"PhotoAppBarButtonStyle")); isVideo = !isVideo; }
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
- 已建议为答案 pjfitren 2012年7月26日 6:35
- 已标记为答案 Jie BaoModerator 2012年8月1日 9:01
-
我想到了两种方法:
1.简单的两个btn放一起,一个Collapsed,另外一个Visible,然后由你的isVideo控制,testBtn->Visibility = Collapsed;
2.查找资源来设置
xaml
<Page.Resources> <Style x:Key="PhotoBtnStyle" TargetType="Button" BasedOn="{StaticResource PhotoAppBarButtonStyle}"/> <Style x:Key="VideoBtnStyle" TargetType="Button" BasedOn="{StaticResource VideoAppBarButtonStyle}"/> </Page.Resources>
cpp:
String^ strRes = isVideo? "PhotoBtnStyle" : "VideoBtnStyle"; testBtn->Style = safe_cast<Windows::UI::Xaml::Style^>(this->Resources->Lookup(strRes)); isVideo= !isVideo;
第2种方法可能还有直接ref new Style(?),这个我不清楚怎么写。
- 已编辑 pjfitren 2012年7月25日 6:07
- 已标记为答案 Jie BaoModerator 2012年8月1日 9:01
全部回复
-
我想到了两种方法:
1.简单的两个btn放一起,一个Collapsed,另外一个Visible,然后由你的isVideo控制,testBtn->Visibility = Collapsed;
2.查找资源来设置
xaml
<Page.Resources> <Style x:Key="PhotoBtnStyle" TargetType="Button" BasedOn="{StaticResource PhotoAppBarButtonStyle}"/> <Style x:Key="VideoBtnStyle" TargetType="Button" BasedOn="{StaticResource VideoAppBarButtonStyle}"/> </Page.Resources>
cpp:
String^ strRes = isVideo? "PhotoBtnStyle" : "VideoBtnStyle"; testBtn->Style = safe_cast<Windows::UI::Xaml::Style^>(this->Resources->Lookup(strRes)); isVideo= !isVideo;
第2种方法可能还有直接ref new Style(?),这个我不清楚怎么写。
- 已编辑 pjfitren 2012年7月25日 6:07
- 已标记为答案 Jie BaoModerator 2012年8月1日 9:01
-
这样即可:
void App13::MainPage::btn1_Click_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) { if(isVideo) safe_cast<Button^>(sender)->Style = safe_cast<Windows::UI::Xaml::Style^>(App::Current->Resources->Lookup(L"VideoAppBarButtonStyle")); else safe_cast<Button^>(sender)->Style = safe_cast<Windows::UI::Xaml::Style^>(App::Current->Resources->Lookup(L"PhotoAppBarButtonStyle")); isVideo = !isVideo; }
Bob Bao [MSFT]
MSDN Community Support | Feedback to us
- 已建议为答案 pjfitren 2012年7月26日 6:35
- 已标记为答案 Jie BaoModerator 2012年8月1日 9:01