繁体   English   中英

VB.Net中的WPF标记扩展无法正常工作

[英]WPF Markup Extension in VB.Net not working

我试图在这篇博文中创建一个VB.Net标记扩展,但是在vb.net中

<Application x:Class="Application"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    ShutdownMode="OnExplicitShutdown">
    <Application.Resources>        
    </Application.Resources>
    <JumpList.JumpList>
        <JumpList ShowRecentCategory="True">
            <JumpTask Title="Save as..." Arguments="-saveas"
                      ApplicationPath="{local:ApplicationFullPath}">
            </JumpTask>
        </JumpList>
    </JumpList.JumpList>
</Application>

但它在扔

错误1未知的构建错误,'键不能为空。 参数名称:键行9位置62.' C:\\ Users \\ jessed.ECREATIVE \\ My Dropbox \\ Projects \\ c2d2 \\ c2d2 \\ Application.xaml 9 62 c2d2

我将示例的c#部分转换为

Public Class ApplicationFullPath
    Inherits Markup.MarkupExtension

    Public Overrides Function ProvideValue(ByVal serviceProvider As System.IServiceProvider) As Object
        Return System.Reflection.Assembly.GetExecutingAssembly.Location()
    End Function

End Class

我错过了什么吗? 任何帮助将不胜感激

我绝不会认真对待这个标记扩展。

相反,这样的事情怎么样:

public partial class App : Application
{
    public static string ApplicationFullPath
    {
        get { return Assembly.GetExecutingAssembly().Location; }
    }

    ...
<JumpTask ApplicationPath="{x:Static local:App.ApplicationFullPath}"/>

顺便说一下,标记扩展类名称应以“扩展”结尾,这可能甚至可以解决您的问题(该类将被称为 ApplicationFullPathExtension ,但XAML中的调用仍将是 ApplicationFullPath

我会遵循HB建议,但除此之外你没有定义上面的“本地”xmlns。 你需要这样的东西:

<Application x:Class="Application"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyNamespace"
    ShutdownMode="OnExplicitShutdown">
    <!-- ... existing stuff -->
</Application>

其中MyNamespace是定义了标记扩展的CLR namesapce。

如果您从链接到的博客下载代码,您可以看到完整的示例,即:

<Application x:Class="Jumplist.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Jumplist"
             StartupUri="MainWindow.xaml">

    <Application.Resources>

    </Application.Resources>

    <JumpList.JumpList>
        <JumpList ShowRecentCategory="True"
                  ShowFrequentCategory="True">
            <JumpTask Title="Say Hello!" 
                      Description="Display Greeting Message" 
                      ApplicationPath="{local:ApplicationFullPath}"
                      Arguments="{x:Static local:ApplicationActions.SayHello}"
                      IconResourcePath="{local:ApplicationFullPath}"
                      IconResourceIndex="0" />

        </JumpList>
    </JumpList.JumpList>

</Application>

请注意,已定义本地xmlns,并且App在“Jumplist”的同一CLR名称空间中定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM