mirror of
https://gitee.com/csharpui/CPF.git
synced 2025-12-19 19:47:12 +08:00
Razor测试
This commit is contained in:
41
CPF.Razor/CpfExtensions.cs
Normal file
41
CPF.Razor/CpfExtensions.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
|
||||
namespace CPF.Razor
|
||||
{
|
||||
public static class CpfExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a component of type <typeparamref name="TComponent"/> and adds it as a child of <paramref name="parent"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TComponent"></typeparam>
|
||||
/// <param name="host"></param>
|
||||
/// <param name="parent"></param>
|
||||
public static void AddComponent<TComponent>(this IHost host, CPF.UIElement parent) where TComponent : IComponent
|
||||
{
|
||||
if (host is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(host));
|
||||
}
|
||||
|
||||
if (parent is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(parent));
|
||||
}
|
||||
|
||||
var services = host.Services;
|
||||
var renderer = new CpfRenderer(services, services.GetRequiredService<ILoggerFactory>());
|
||||
|
||||
//// TODO: This call is an async call, but is called as "fire-and-forget," which is not ideal.
|
||||
//// We need to figure out how to get Xamarin.Forms to run this startup code asynchronously, which
|
||||
//// is how this method should be called.
|
||||
renderer.AddComponent<TComponent>(new ElementHandler(renderer, parent)).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user