UWP xamarin中的自定义渲染器
我有在UWP xamarin中的自定义渲染器,xamarin,xamarin.forms,xamarin.ios,xamarin.uwp,Xamarin,Xamarin.forms,Xamarin.ios,Xamarin.uwp,我有在iOSxamarin表单上进行自定义渲染的代码,但我希望类似的UWP using System; using MobileAssessment.Interface; using MobileAssessment.iOS; using MobileAssessment.Pages; using UIKit; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; using iOSPlatform = Xamarin.Forms.Plat
iOS
xamarin表单上进行自定义渲染的代码,但我希望类似的UWP
using System;
using MobileAssessment.Interface;
using MobileAssessment.iOS;
using MobileAssessment.Pages;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using iOSPlatform = Xamarin.Forms.Platform.iOS.Platform;
[assembly: Xamarin.Forms.Dependency(typeof(ILoadingPageServiceiOS))]
namespace MobileAssessment.iOS
{
public class ILoadingPageServiceiOS : ILodingPageService
{
private UIView _nativeView;
private bool _isInitialized;
public void ShowLoadingPage()
{
// check if the user has set the page or not
if (!_isInitialized)
InitLoadingPage(new LoadingIndicatorPage()); // set the default page
// showing the native loading page
UIApplication.SharedApplication.KeyWindow.AddSubview(_nativeView);
}
public void HideLoadingPage()
{
_nativeView.RemoveFromSuperview();
}
public void InitLoadingPage(ContentPage loadingIndicatorPage = null)
{
// check if the page parameter is available
if (loadingIndicatorPage != null)
{
// build the loading page with native base
loadingIndicatorPage.Parent = Xamarin.Forms.Application.Current.MainPage;
loadingIndicatorPage.Layout(new Rectangle(0, 0,
Xamarin.Forms.Application.Current.MainPage.Width,
Xamarin.Forms.Application.Current.MainPage.Height));
var renderer = loadingIndicatorPage.GetOrCreateRenderer();
_nativeView = renderer.NativeView;
_nativeView.BackgroundColor = UIColor.Black;
_nativeView.Alpha = (System.nfloat)0.7;
_isInitialized = true;
}
}
}
internal static class PlatformExtension
{
public static IVisualElementRenderer GetOrCreateRenderer(this VisualElement bindable)
{
var renderer = iOSPlatform.GetRenderer(bindable);
if (renderer == null)
{
renderer = iOSPlatform.CreateRenderer(bindable);
iOSPlatform.SetRenderer(bindable, renderer);
}
return renderer;
}
}
}
我试过下面的UWP代码
using System;
using MobileAssessment.Interface;
using MobileAssessment.UWP;
using MobileAssessment.Pages;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.UWP;
using uwpPlatform = Xamarin.Forms.Platform.UWP.Platform;
[assembly: Xamarin.Forms.Dependency(typeof(ILoadingPageServiceRenderer))]
namespace MobileAssessment.UWP
{
public class ILoadingPageServiceRenderer : ILodingPageService
{
private View _nativeView;
private bool _isInitialized;
public void ShowLoadingPage()
{
// check if the user has set the page or not
if (!_isInitialized)
InitLoadingPage(new LoadingIndicatorPage()); // set the default page
// showing the native loading page
Application.SharedApplication.KeyWindow.AddSubview(_nativeView);
}
public void HideLoadingPage()
{
_nativeView.RemoveFromSuperview();
}
public void InitLoadingPage(ContentPage loadingIndicatorPage = null)
{
// check if the page parameter is available
if (loadingIndicatorPage != null)
{
// build the loading page with native base
loadingIndicatorPage.Parent = Xamarin.Forms.Application.Current.MainPage;
loadingIndicatorPage.Layout(new Rectangle(0, 0,
Xamarin.Forms.Application.Current.MainPage.Width,
Xamarin.Forms.Application.Current.MainPage.Height));
var renderer = loadingIndicatorPage.GetOrCreateRenderer();
_nativeView = renderer.NativeView;
_nativeView.BackgroundColor = Color.Black;
_nativeView.Alpha = (System.nfloat)0.7;
_isInitialized = true;
}
}
}
internal static class PlatformExtension
{
public static IVisualElementRenderer GetOrCreateRenderer(this VisualElement bindable)
{
var renderer = uwpPlatform.GetRenderer(bindable);
if (renderer == null)
{
renderer = iOSPlatform.CreateRenderer(bindable);
uwpPlatform.SetRenderer(bindable, renderer);
}
return renderer;
}
}
}
上述代码实际用于从中获取的活动指示器
但是论坛不共享相同的UWP解决方案,所以我创建了我的windows代码。如果您正在寻找“全屏加载”样式,您也可以使用ControlTemplate并将其绑定到特定页面
您可以这样做:
<Color x:Key="AccentColor">#ABDEBE</Color>
<ControlTemplate x:Key="FullScreenLoadingTemplate">
<AbsoluteLayout>
<ContentPresenter
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0, 0, 1, 1" />
<ContentView
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
BackgroundColor="#80FFFFFF"
IsVisible="{TemplateBinding BindingContext.IsBusy}">
<ActivityIndicator
HorizontalOptions="Center"
IsRunning="{TemplateBinding BindingContext.IsBusy}"
VerticalOptions="Center"
Color="{StaticResource AccentColor}" />
</ContentView>
</AbsoluteLayout>
</ControlTemplate>
#ABDEBE
和页面绑定:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
ControlTemplate="{StaticResource FullScreenLoadingTemplate}"
Title="{Binding Title}"
IsBusy="{Binding IsBusy}"
x:Class="TestApp.Mobile.Pages.TestPage">... </ContentPage>
。。。
毕竟,我没能为Windows平台编写相同的代码。所以我用Rg.Plugins.Popup.Pages插件制作了解决方案
应用程序全局加载程序指示器xaml文件
<?xml version="1.0" encoding="UTF-8"?>
<pages:PopupPage xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:telerikPrimitives="clr-namespace:Telerik.XamarinForms.Primitives;assembly=Telerik.XamarinForms.Primitives"
x:Class="MobileAssessment.Pages.AppGlobalLoader">
<ContentPage.Content>
<telerikPrimitives:RadBusyIndicator x:Name="BusyIndicator"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
AnimationContentHeightRequest="100"
AnimationContentWidthRequest="100"
AnimationType="Animation8"
AnimationContentColor="Teal"
IsBusy="True"
BackgroundColor="Black"
Opacity="0.7">
<telerikPrimitives:RadBusyIndicator.Content>
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Opacity="0.7">
</StackLayout>
</telerikPrimitives:RadBusyIndicator.Content>
</telerikPrimitives:RadBusyIndicator>
</ContentPage.Content>
</pages:PopupPage>
我在基本导航器类中创建了两个方法show和hide popup
public async Task showLoader()
{
isPopupOpen = true;
AppGlobalLoader theAppGlobalLoader = new AppGlobalLoader();
await _Navigation?.PushPopupAsync(theAppGlobalLoader);
}
public async Task hideLoader()
{
if (isPopupOpen)
{
await _Navigation?.PopPopupAsync(true);
}
}
现在我在长时间运行的任务中使用上述方法
async Task ExecuteAndSave()
{
if (ValidateFields())
{
await base.showLoader();
//Your long ruuning task dealing with server and saving stuff in database
await base.hideLoader();
await App.Current.MainPage.DisplayAlert("Successful", "Created", "Ok");
await ExecuteBackCommand();
}
}
不,我不希望每个屏幕上都有机具指示灯。我想创建一个服务的指标,并使用它时,我想在应用程序。基本上,对于iOS实现,它会在主应用程序UIView窗口中添加(作为UIView的指示符)。但我不知道UWP。
async Task ExecuteAndSave()
{
if (ValidateFields())
{
await base.showLoader();
//Your long ruuning task dealing with server and saving stuff in database
await base.hideLoader();
await App.Current.MainPage.DisplayAlert("Successful", "Created", "Ok");
await ExecuteBackCommand();
}
}