开发者

IRootObjectProvider not available in Designer?

开发者 https://www.devze.com 2023-03-14 23:09 出处:网络
I\'m a bit of a WPF newbie, so go easy on me please folks! I am trying to implement a MarkupExtension, in which I want to reference the root element of the XAML which is calling the extension (I need

I'm a bit of a WPF newbie, so go easy on me please folks!

I am trying to implement a MarkupExtension, in which I want to reference the root element of the XAML which is calling the extension (I need its Name element to do a lookup).

The following code works fine at run time, but during design time I get an error in the designer window.

[MarkupExtensionReturnTypeAttribute(typeof(string))]
public class TranslateExtension : MarkupExtension
{

    public TranslateExtension(string key)
    {
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
   开发者_如何学Go {
        var root = ((System.Xaml.IRootObjectProvider)serviceProvider).RootObject as FrameworkElement;
        return root.Name;
    }

}

gives me an error in designer of:

Unable to cast object of type 'InstanceBuilderServiceProvider' to type 'System.Xaml.IRootObjectProvider'.

Which is sort of self explanatory - obviously ProvideValue is being called with a service provider which can't be converted to RootObjectProvider.

Is there any work around? Or am I doing something really silly?


Aren't you supposed to do something like

var service = (IRootObjectProvider)serviceProvider.GetService(typeof(IRootObjectProvider));

It still does not help with designer however...


Try this:

var rootObjectProvider = serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;


IRootObjectProvider is not available in WPF 4 as of now, it will be included in future. You can programmatically use Xaml Services that supports IRootObjectProvider but WPF does not support it yet.

0

精彩评论

暂无评论...
验证码 换一张
取 消