編輯:我找到了一個重現問題的最簡單方法。開始在Visual Studio中的一個新的Windows Phone應用程序模板,並將其加入到您的MainPage.xaml
:Windows Phone:在C#中應用XAML樣式'重置'本身
<Page.Resources>
<Style x:Name="TileStyle" TargetType="Button">
<Setter Property="BorderThickness" Value="0,0,0,0" />
</Style>
</Page.Resources>
<Button x:Name="btn" Style="{StaticResource TileStyle}" />
,並給你的代碼隱藏文件(在OnNavigatedTo
事件處理程序):
btn.Style = TileStyle;
重現問題當您導航到頁面時。
原始文本:我正在嘗試創建一個Windows Phone應用程序,它是Whack-A-Mole的最小版本。在我的遊戲頁面上,我有兩種用XAML編寫的按鈕樣式,代表一個洞被佔用或未被佔用。
<Page.Resources>
<Style x:Name="TileStyle" TargetType="Button">
<Setter Property="BorderThickness" Value="0,0,0,0" />
<Setter Property="Width" Value="125"/>
<Setter Property="Height" Value="125" />
</Style>
<Style x:Name="CircleStyle" TargetType="Button"
BasedOn="{StaticResource TileStyle}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Rectangle Fill="{TemplateBinding Background}" />
<Image Source="/Assets/white_circle.png" />
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<!--later on...-->
<Grid>
<Button Style="{StaticResource TileStyle}" Click="Button_Click"/>
<Button Style="{StaticResource TileStyle}" Click="Button_Click"/>
<Button Style="{StaticResource TileStyle}" Click="Button_Click"/>
...
</Grid>
第一種風格基本上只是一個空白的黑色125x125平方米;你只能通過點擊它來告訴它。第二種風格是BasedOn,它顯示white_circle.png代替空白方塊。在我的代碼隱藏的C#文件中,我有兩種使用這些樣式的方法:一種是在頁面加載時調用,另一種在單擊按鈕時調用。
protected async void OnNavigatedTo(NavigationEventArgs e)
while (!hasUserWonGame)
//sleeps for 1-3 secs
//decides where circle should go at by generating random int
b.Style = CircleStyle; //this works fine!
//more logic, waits
if (!hasUserWonRound)
b.Style = TileStyle; //reverts to TileStyle (this does not work!)
private void Button_Click(object sender, RoutedEventArgs e)
//decides if user is clicking on a circle
if (areTilesDisplayingCircle[index - 1])
//user has won the round!
hasUserWonRound = true;
b.Style = TileStyle; //this also does not work!
//more logic, updates score, decides if user has won the entire game
當用戶第一次導航到頁面時,這些按鈕都是黑色的,並且它們應該是不可見的。白色圓圈按預期出現在空間中。但是,當它從現場消失時,圓形會留下未應用任何樣式的按鈕。 (見圖片)。
您應該也可以在'TileStyle'中爲'Template'屬性設置一個Setter。 – Clemens 2015-02-09 08:25:35
其實,你沒有附加任何圖像到你的文章。就實際問題而言,您需要發佈[a _minimal_,_complete_代碼示例](http://stackoverflow.com/help/mcve)。我根據你發佈的內容寫了一個簡單的例子,它對我來說工作得很好。我只需設置一個計時器,通過按鈕循環,每次設置一個具有'CircleStyle'風格,並將之前設置的一個設置回''TileStyle'',而不是處理鼠標點擊。即使重新設置爲「TileStyle」,該按鈕仍保持空白,並且看起來不像普通按鈕。 – 2015-02-09 09:46:03
你能告訴我們你的'TileStyle'屬性的代碼嗎? – 2015-02-09 14:52:10