WPF Markup Extension in VB.Net not working
I'm trying to creat开发者_StackOverflow中文版e a VB.Net Markup Extension per this blog post but in 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>
but it is throwing
Error 1 Unknown build error, 'Key cannot be null. Parameter name: key Line 9 Position 62.' C:\Users\jessed.ECREATIVE\My Dropbox\Projects\c2d2\c2d2\Application.xaml 9 62 c2d2
I converted the c# portion of the example to
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
am I missing something? Any help would be greatly appreciated
I would never use a markup extension for this, seriously.
How about something like this instead:
public partial class App : Application
{
public static string ApplicationFullPath
{
get { return Assembly.GetExecutingAssembly().Location; }
}
...
<JumpTask ApplicationPath="{x:Static local:App.ApplicationFullPath}"/>
(Markup extension class names should end with "Extension" by the way, maybe that would even fix your problem (the class would be called ApplicationFullPathExtension , the call in XAML would still be ApplicationFullPath though))
I would follow H.B. suggestion, but aside from that you don't define the "local" xmlns above. You would need something like:
<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>
Where MyNamespace is the CLR namesapce that your markup extension is defined in.
If you download the code from the blog that you link to you can see the complete example, which is:
<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>
Notice that both the local xmlns is defined, and the App is defined in the same CLR namespace of "Jumplist".
精彩评论