我在 app.xaml.cs 中创建了一个CParametres对象,如下所示:
public partial class App : Application
{
public CParametres myParamObject;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
myParamObject = new CParametres(System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) +@"\BingMapsParam.ini");
if (myParamObject.LoadParams() == false)
{
return;
}
Resources.Add("myParamObject", myParamObject);
}
}
现在,在我的app.xaml中,我添加了一个词典:
<Application x:Class="myGeoloc.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
而且,这是我的Dictionnary:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:t="clr-namespace:myGeoloc"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF">
<t:CParametres x:Key="myParamObject"/>
<Style TargetType="m:Pushpin" x:Key="PushpinStyle_Fournisseur">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="m:Pushpin" >
<!-- <Image Stretch="Fill" Source="C:\Users\FabioWalter\Documents\Visual Studio 2013\Projects\myGeoloc\myGeoloc\bin\Debug\Pushpins\PushPinStandard.png" />-->
<Image Stretch="Fill" Source="{Binding Path=strPicturePushpinFournisseur, Source={StaticResource myParamObject}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Width" Value="64" />
<Setter Property="Height" Value="64" />
</Style>
</ResourceDictionary>
strPicturePushpinFournisseur是CParametres中的字符串。该字符串包含图片路径。
实际上,图片无法显示,而且与我的不良约束相关。
有人可以帮我吗? 有什么想法吗?
非常感谢:)
答案 0 :(得分:0)
你可以这样做:
将对象放入资源字典中以使其可供应用程序的其余部分使用
<Application x:Class="SomeNameSpace.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:t="clr-namespace:SomeNameSpace.NameSpaceForT"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="View/DictionaryResources/MainResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
<t:CParametres x:Key="myParamObject"/>
</ResourceDictionary>
</Application.Resources>
</Application>
绑定任何对象的属性(这是一个示例):
<Window x:Class="SomeNameSpace.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:View="clr-namespace:SomeNameSpace.View"
Title="MainWindow"
DataContext="{Binding Path=strPicturePushpinFournisseur, Source={StaticResource myParamObject}}"
Height="350" Width="525">
...
修改强>
您可以在App.xaml.cs
文件中创建对象,并以这种方式添加构造函数参数:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
Resources.Add("myParamObject", new CParametres ("Param1", "Param2"));
}
}
以这种方式使用它:
<TextBlock Text="{Binding Path=strPicturePushpinFournisseur, Source={StaticResource myParamObject}}"></TextBlock>
希望它有所帮助...