0
我編程主要是基於C#代碼WPF類庫和我目前正在試圖加載XAML文件只對造型UI元素。WPF類庫,加載XAML絕對URI或相對URI既沒有工作
這裏是XAML的 「風格」 代碼 「BuildAction的:內容」:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1">
<Style x:Key="LabelStyle" TargetType="{x:Type Label}">
<Setter Property="Height" Value="53" />
<Setter Property="Width" Value="130" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Margin" Value="99,71,0,0" />
<Setter Property="VerticalAlignment" Value= "Top" />
<Setter Property="Foreground" Value="#FFE75959" />
<Setter Property="FontFamily" Value="Calibri" />
<Setter Property="FontSize" Value="40" />
</Style>
這裏是我的標籤代碼:
private void CreateElement(int i)
{
UIElementOut[i] = new Label();
var uiElement = (Label)UIElementOut[i];
uiElement.HorizontalAlignment = HorizontalAlignment.Center;
uiElement.VerticalAlignment = VerticalAlignment.Center;
uiElement.FontFamily = new FontFamily(FFontInput[i]);
uiElement.FontSize = Convert.ToDouble(FontSizeIn[i]);
uiElement.Content = TextIn[i];
Brush BgBrushColor = new SolidColorBrush(RGBAToMediaColor(FBgCol[i]));
Brush FgBrushColor = new SolidColorBrush(RGBAToMediaColor(FFgCol[i]));
uiElement.Background = BgBrushColor;
uiElement.Foreground = FgBrushColor;
Uri uri = new Uri("pack://application:,,,/WpfApplication1;component/WpfApplication1/Styles/LabelStyle.xaml", UriKind.Relative);
StreamResourceInfo info = Application.GetContentStream(uri);
System.Windows.Markup.XamlReader reader = new System.Windows.Markup.XamlReader();
ResourceDictionary myResourceDictionary = Application.LoadComponent(uri) as ResourceDictionary;
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
Style myLabelStyle = myResourceDictionary["LabelStyle"] as Style;
uiElement.Style = myLabelStyle;
}
如果UriKind設爲「相對」 我收到此錯誤信息:
A relative URI cannot be created because the 'uriString' parameter represents an absolute URI.
但如果Urikind設置爲「絕對」然後我得到這個:
Cannot use absolute URI.
所以在這兩個反正XAML文件未加載,而不是應用的樣式。
編輯:
我想這個URI:
pack://application:,,,/WpfApplication1;component/Styles/LabelStyle.xaml
,並獲得同樣的錯誤。
閱讀包URI方案:https://msdn.microsoft.com/en-us/library/vstudio/aa970069(v=vs.100).aspx – AnjumSKhan
我想這已經是我發現這給了一個職位類似的答案,但我仍然有同樣的錯誤。 – lecloneur
您的ResourceDictionary是在同一個項目中還是您在此處引用的另一個項目? – AnjumSKhan