WebView.AddWebAllowedObject(String, Object) 메서드

정의

네이티브 Windows 런타임 개체를 WebView 내의 최상위 문서에 전역 매개 변수로 추가합니다.

public:
 virtual void AddWebAllowedObject(Platform::String ^ name, Platform::Object ^ pObject) = AddWebAllowedObject;
void AddWebAllowedObject(winrt::hstring const& name, IInspectable const& pObject);
public void AddWebAllowedObject(string name, object pObject);
function addWebAllowedObject(name, pObject)
Public Sub AddWebAllowedObject (name As String, pObject As Object)

매개 변수

name
String

Platform::String

winrt::hstring

WebView에서 문서에 노출할 개체의 이름입니다.

pObject
Object

Platform::Object

IInspectable

WebView에서 문서에 노출할 개체입니다.

예제

이 예제에서는 AllowForWeb 특성으로 클래스를 데코레이트하는 방법을 보여 주는 예제입니다.

using Windows.Foundation.Metadata;

namespace MyRuntimeComponent
{
    [AllowForWeb]
    public sealed class MyNativeClass
    {
        public void NativeMethod()
        {
            ...
        }

        ...
    }
}

이 예제에서는 탐색 시작 이벤트를 사용하여 탐색이 시작될 때 개체를 삽입 하는 방법을 보여 줍니다.

<WebView x:Name="webView" Source="https://www.contoso.com/index.html"
         NavigationStarting="webView_NavigationStarting"/>
private void webView_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args) 
{ 
    if (args.Uri.Host == "www.contoso.com")  
    { 
        webView.AddWebAllowedObject("nativeObject", new MyNativeClass()); 
    } 
} 

웹 페이지의 스크립트에서 네이티브 개체에 액세스하는 방법은 다음과 같습니다.

<script type='text/javascript'>
    nativeObject.nativeMethod(); // Call the projected WinRT method.
</script>

설명

이 메서드를 사용하여 WebView 내 최상위 문서의 컨텍스트에서 네이티브 Windows 런타임 개체를 전역 매개 변수로 노출합니다. Windows 런타임 개체를 프로젝션하려면 민첩하고 AllowForWeb 특성으로 데코레이팅되어야 합니다.

참고

Microsoft Visual Basic, C# 또는 Visual C++ 구성 요소 확장(C++/CX)을 사용하여 만든 런타임 클래스는 기본적으로 민첩합니다. 자세한 내용은 다중 스레드 환경에서 스레딩 및 마샬링Windows 런타임 개체 사용을 참조하세요.

AddWebAllowedObject에 전달된 개체는 앱 어셈블리와 별개인 Windows 런타임 구성 요소에서 가져와야 합니다. 이는 AllowForWeb 특성이 WebView 보안 하위 시스템에 의해 식별되는 속성이 되도록 하는 데 필요합니다. 앱 프로젝트에서 클래스를 사용하는 경우 AddWebAllowedObject가 작동하지 않습니다.

WebView가 네이티브 개체에 액세스하는 새 페이지로 이동할 때마다 AddWebAllowedObject를 호출해야 합니다. WebView.NavigationStarting 이벤트를 사용하여 탐색이 시작될 때 개체를 삽입할 수 있습니다.

적용 대상