2013-05-27 28 views
0

我搜索並嘗試了許多操作來完成此操作。我有一個成功做XAML代碼像這樣綁定到多個XAML框架風格,樣式的名稱是ViewPersonTextboxDataStyle:如何從後面代碼中的Common/StandardStyles.xaml中指定XAML Stype

<TextBox Grid.Row="5" Grid.Column="1" Name="textboxName" Text="{Binding textboxName}" Margin="5,5,5,5" Style="{StaticResource ViewPersonTextboxDataStyle}"/> 

所以,當我到達另一個框架,我想創建網格行並在代碼定義的背後,我想這一點,我認爲是正確的:

var resourceDictionary = new ResourceDictionary() 
{ 
    Source = new Uri("ms-appx:///Common/StandardStyles.xaml", UriKind.Absolute) 
}; 
var style = resourceDictionary["ViewPersonTextboxDataStyle"] as Style; 
textBlock.Style = resourceDictionary["ViewPersonTextboxDataStyle"] as Style; 

所以在這一點上,我可以看到在資源字典中發現的樣式正確填充樣式。但是分配到textBlock.Style會導致一個災難性異常(Catastrophic Exception)。所以,要麼我錯過了一個步驟,要麼這是不正確的。

沒有很多關於這方面的淨信息。

回答

0

試試這個:

textBlock.style = this.Resources["ViewPersonTextboxDataStyle"] as Style; 

你不必使用資源字典

編輯:

this.Resources指Page.Resources(即萬一ViewPersonTextboxDataStyle在頁面定義我們用這個資源)

試試這個:

textBlock.style = App.Current.Resources["ViewPersonTextboxDataStyle"] as Style; 
+0

這仍然給[System.Runtime.InteropServices.COMException] = {「錯誤HRESULT E_FAIL已從調用COM組件返回。」} – RallyRabbit

+0

實際上,這是因爲this.Resources沒有密鑰。 – RallyRabbit

+0

@RallyRabbit:我編輯了我的答案。請檢查 – Raghavendra

1

好的,謝謝你Raghavendra,這確實指出我在正確的方向告訴我,我正在嘗試的事情沒有脫離基地。

我結束了是:

style = Application.Current.Resources["ViewPersonTextDataStyle"] as Style; 
textBlock.Style = style; 

Raghavendra是正確的,你不需要使用資源管理器,你也不必在本地XAML來定義它。無論如何,我使用上面的這一行來完成每一幀的分配。

因此,我的例外是一個IDIOT(即我)。我應該一直使用我的TEXTBLOCK風格而不是我的TEXTBOX風格。將文本框樣式分配給文本塊導致異常。

相關問題