在dll中查找资源而不是主应用程序

时间:2016-04-17 13:05:22

标签: c# wpf xaml dll

我正在开发一个DLL,它会在WPF应用程序中放置两个隐藏按钮。

点击正确顺序上的这些按钮会引发一个事件。

要删除Mouse Over效果,我创建了一个新样式。我希望按钮完全透明。

WPF代码

    SecretCode.WPF secretCode = new SecretCode.WPF(testGrid,  SecretCode.WPF.Location.Bottom, 75, 4000);
    secretCode.SecretCodeActivated += secretCode_SecretCodeActivated;

的App.xaml

 <Style x:Key="TransparentStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border>
                    <Border.Style>
                        <Style TargetType="{x:Type Border}">
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="Transparent"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                    <Grid Background="Transparent">
                        <ContentPresenter></ContentPresenter>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然而,问题在于我不想将该样式放在WPF项目上,因为我希望DLL完全独立。

密码DLL

public WPF(Grid grid, Location location, int size, int timeout)
{
    Button leftButton = new Button();
    leftButton.Width = size;
    leftButton.Height = size;
    leftButton.Margin = new Thickness(0, 0, 0, 0);
    leftButton.HorizontalAlignment = HorizontalAlignment.Left;
    leftButton.VerticalAlignment = location == Location.Top? VerticalAlignment.Top : VerticalAlignment.Bottom;
    leftButton.Background = Brushes.Transparent;
    leftButton.Style = Application.Current.FindResource("TransparentStyle") as Style;
    leftButton.Click += leftPanel_Click;
    grid.Children.Add(leftButton);

    Button rightButton = new Button();
    rightButton.Width = size;
    rightButton.Height = size;
    rightButton.Margin = new Thickness(0, 0, 0, 0);
    rightButton.HorizontalAlignment = HorizontalAlignment.Right;
    rightButton.VerticalAlignment = location == Location.Top ? VerticalAlignment.Top : VerticalAlignment.Bottom;
    rightButton.Background = Brushes.Transparent;
    rightButton.Style = Application.Current.FindResource("TransparentStyle") as Style;
    rightButton.Click += rightPanel_Click;
    grid.Children.Add(rightButton);

    timeoutTimer.Interval = timeout;
    timeoutTimer.Elapsed += timeoutTimer_Tick;
}

有没有办法做到这一点?

3 个答案:

答案 0 :(得分:1)

在某些DLL中:

<ResourceDictionary >
    <Style x:Key="BoringButtonStyle" TargetType="{x:Type Button}">
        //.....
    </Style>
</ResourceDictionary>

在您的申请表上:

<Application  ...>
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Globals;component/Styles/ButtonStyles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

然后你可以自由地将它用作DynamicResource:

<UserControl ...>
    <Grid>
        <Button style="{DynamicResource BoringButtonStyle}"/>
    </Grid>
</UserControl>

app.xaml中代码的另一种方法是:

ResourceDictionary dict = new ResourceDictionary(); 
System.Windows.Application.LoadComponent(dict, new System.Uri("/SomeAssembly;component/SomeResourceDictionary.xaml",System.UriKind.Relative));

答案 1 :(得分:0)

通常,在创建库时,可以定义其设置集。这些设置是通过方法指定的。但是,您需要以最常用的方式表达您的问题。您需要一种方法来设置预期的模式(正确的顺序),一个历史成员来检查模式是否匹配以及样式/类属性。您可以将设置存储到ini文件或XML中,也可以存储到程序的内存中或DLL内部。

答案 2 :(得分:0)

这是我能想到的。希望这可以帮助。 在DLL类中使用以下字符串。

private string xamlText = "<Style xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Key=\"TransparentStyle\" TargetType=\"Button\">" +
                                "<Setter Property=\"Template\">" +
                                   " <Setter.Value> " +
                                        "<ControlTemplate TargetType = \"Button\">" +
                                            "<Border>" +
                                                "<Border.Style>" +
                                                    "<Style TargetType=\"{x:Type Border}\">" +
                                                        "<Style.Triggers>" +
                                                            "<Trigger Property = \"IsMouseOver\" Value=\"True\">" +
                                                                "<Setter Property = \"Background\" Value=\"Transparent\"/>" +
                                                            "</Trigger>" +
                                                        "</Style.Triggers>" +
                                                    "</Style>" +
                                                "</Border.Style>" +
                                                "<Grid Background = \"Transparent\">" +
                                                    "<ContentPresenter></ContentPresenter>" +
                                                "</Grid>" +
                                            "</Border>" +
                                        "</ControlTemplate>" +
                                    "</Setter.Value>" +
                                "</Setter>" +
                            "</Style>";

在WPF方法的顶部添加以下代码

var stringReader = new StringReader(xamlText);
var xmlReader = XmlReader.Create(stringReader);
var style = XamlReader.Load(xmlReader) as Style;

接下来,就这样做:

leftButton.Style = style;

rightButton.Style = style;

我曾经有一个极端的情况我不得不使用这种方法所以我把它拉了出来。