C#类属性在Javascript中不可访问 [英] C# class attributes not accessible in Javascript

查看:261
本文介绍了C#类属性在Javascript中不可访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过一个C#WebView控件在JavaScript中创建一个类。
因此,我使用WebView.AddWebAllowedObject方法。然而,如果我分配一个属性,它工作正常,但如果我分配整个类来获取所有属性在js,所有的属性(和方法btw)是未定义。我尝试了在www中找到的一切。请参阅附加的代码:

  //我想要访问的类
[AllowForWeb,ComVisible(true)]
[MarshalingBehavior(MarshalingType.Agile)]
public class DeviceInformation
{
public string IPAdress {get;私人集}

public DeviceInformation()
{
IPAdress = GetIPAdress();
}

public string GetDeviceUUID()
{
EasClientDeviceInformation deviceinfo = new EasClientDeviceInformation();
return deviceinfo.Id.ToString();
}

public string GetIPAdress()
{
List< string> ipAddresses = new List< string>();
var hostnames = NetworkInformation.GetHostNames();
foreach(host names中的var hn)
{
if(hn?.IPInformation!= null&&(hn.IPInformation.NetworkAdapter.IanaInterfaceType == 71 ||
hn.IPInformation.NetworkAdapter.IanaInterfaceType == 6))
{
string ipadress = hn.DisplayName;
return ipadress;
}
}
return string.Empty;
}
}

这里的对象被初始化。

  DeviceInformation devinf = new DeviceInformation(); 
private void View_NavigationStarting(WebView sender,WebViewNavigationStartingEventArgs args)
{
if(args.Uri.Host ==)
{
// win_ipadress有一个ipadress作为值
view.AddWebAllowedObject(win_ipadress,devinf.IPAdress);
// deviceInformation也初始化了,但是我没有访问它的属性
view.AddWebAllowedObject(deviceInformation,devinf);
}
}

这是我在js中调用的方式:

  else if($ .os.ie){
myIpAdr = window.win_ipadress;
//抛出一个异常,因为GetIPAdress()是undefined
myIpAdr = window.deviceInformation.GetIPAdress();
}

我在Windows通用应用程序中使用。 Javascript和在WebView中显示的HTML代码已经在Android的iOS上使用。

解决方案

我相信你需要定义方法名以小写字符开头。



例如:将GetIPAddress更改为getIPAddress。



它在我的身边,发现如果我使用大写名称GetIPAddress,它将无法工作。但是如果我使用getIPAddress,它的工作原理。



在我阅读kangax的解释在 [Update]

/ p>

由于在对方法名进行更改后它仍然不起作用,我认为问题应该与如何公开Windows运行时对象相关。我想你简单地定义了DeviceInformation类,并试图在同一个项目中使用它。



首先,我们需要创建一个单独的windows通用Windows运行时组件项目。 p>

应该将c#类DeviceInformation放入此项目。保持相同的代码。



然后,在通用应用程序项目中,添加对Windows运行时组件的引用,并保留休息代码以使用Windows运行时对象。



[Update 2]



只是注意到VS中的一个有趣的行为。无论我们在C#中定义的方法名称是以大写还是小写开头,visual studio intellisense都显示小写字母,因此当我们尝试在js中使用方法名称时,它会自动转换。


I want to make a class of mine accessible in JavaScript via a C# WebView-Control. Therefore I am using the WebView.AddWebAllowedObject method. However if I assign an attribute, it works fine, but if I assign the whole class to get all attributes in js, all of the attributes(and methods btw) are "undefined". I tried everything I found in the www. See the attached code:

        //The class I want to make accessible
        [AllowForWeb, ComVisible(true)]
        [MarshalingBehavior(MarshalingType.Agile)]
        public class DeviceInformation
        {
            public string IPAdress { get; private set; }

            public DeviceInformation()
            {
                IPAdress = GetIPAdress();
            }

            public string GetDeviceUUID()
            {
                EasClientDeviceInformation deviceinfo = new EasClientDeviceInformation();
                return deviceinfo.Id.ToString();
            }

            public string GetIPAdress()
            {
                List<string> ipAddresses = new List<string>();
                var hostnames = NetworkInformation.GetHostNames();
                foreach (var hn in hostnames)
                {
                    if (hn?.IPInformation != null && (hn.IPInformation.NetworkAdapter.IanaInterfaceType == 71 ||
                        hn.IPInformation.NetworkAdapter.IanaInterfaceType == 6))
                    {
                        string ipadress = hn.DisplayName;
                        return ipadress;
                    }
                }
                return string.Empty;
            }
        }

Here the objects are initialized.

DeviceInformation devinf = new DeviceInformation();
private void View_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
    {
        if (args.Uri.Host == "")
        {
            //win_ipadress has an ipadress as value
            view.AddWebAllowedObject("win_ipadress", devinf.IPAdress);
            //deviceInformation is initialized as well but I have no access to its attributes
            view.AddWebAllowedObject("deviceInformation", devinf);
        }
    }

That's the way i call it in js:

else if ($.os.ie) {
                    myIpAdr = window.win_ipadress;
                    //Throws an exception because GetIPAdress() is "undefined"
                    myIpAdr = window.deviceInformation.GetIPAdress();
                }

I am using this in a Windows Universal App. The Javascript and in the WebView displayed HTML-Code is already in use for Android an iOS.

解决方案

I believe you need to define the method name starting with a lower case character.

For example: change GetIPAddress to getIPAddress.

I tested it on my side and found if I use the upper case name 'GetIPAddress', it won't work. But if I use getIPAddress, it works.

And after I read kangax's explanation in this thread, I think it makes sense.

[Update]

Since it still doesn't work after you make the change on method name, I think the issue should be related to how you expose the windows runtime object. I guess you simply defined the DeviceInformation class and tried to use it in the same project.

First, we need to create a separate windows universal windows runtime component project.

The c# class DeviceInformation should be put into this project. Keep the same code.

Then, in your universal app project, add reference to the windows runtime component and keep rest code to consume the windows runtime object.

[Update 2]

Just noticed an interesting behavior in VS. No matter if the Method name we defined in C# is starting with uppercase or lowercase, the visual studio intellisense shows the lowercase, so the method name will be automatically converted when we try to use it in js.

这篇关于C#类属性在Javascript中不可访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆