-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
RootComponent.cs
62 lines (53 loc) · 2.42 KB
/
RootComponent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.WebView;
namespace Microsoft.AspNetCore.Components.WebView.Maui
{
/// <summary>
/// Describes a root component that can be added to a <see cref="BlazorWebView"/>.
/// </summary>
public class RootComponent
{
/// <summary>
/// Gets or sets the CSS selector string that specifies where in the document the component should be placed.
/// This must be unique among the root components within the <see cref="BlazorWebView"/>.
/// </summary>
public string? Selector { get; set; }
/// <summary>
/// Gets or sets the type of the root component. This type must implement <see cref="IComponent"/>.
/// </summary>
public Type? ComponentType { get; set; }
/// <summary>
/// Gets or sets an optional dictionary of parameters to pass to the root component.
/// </summary>
public IDictionary<string, object?>? Parameters { get; set; }
internal Task AddToWebViewManagerAsync(WebViewManager webViewManager)
{
// As a characteristic of XAML,we can't rely on non-default constructors. So we have to
// validate that the required properties were set. We could skip validating this and allow
// the lower-level renderer code to throw, but that would be harder for developers to understand.
if (string.IsNullOrWhiteSpace(Selector))
{
throw new InvalidOperationException($"{nameof(RootComponent)} requires a value for its {nameof(Selector)} property, but no value was set.");
}
if (ComponentType is null)
{
throw new InvalidOperationException($"{nameof(RootComponent)} requires a value for its {nameof(ComponentType)} property, but no value was set.");
}
var parameterView = Parameters == null ? ParameterView.Empty : ParameterView.FromDictionary(Parameters);
return webViewManager.AddRootComponentAsync(ComponentType, Selector, parameterView);
}
internal Task RemoveFromWebViewManagerAsync(WebViewManager webviewManager)
{
if (string.IsNullOrWhiteSpace(Selector))
{
throw new InvalidOperationException($"{nameof(RootComponent)} requires a value for its {nameof(Selector)} property, but no value was set.");
}
return webviewManager.RemoveRootComponentAsync(Selector);
}
}
}