C# 如何在窗体上使用组件?
我从Xamarin的组件商店得到了一个组件,Android和iOS都可以使用它 如何在共享/可移植项目(表单)上使用此组件? 或者我可以将表单内容与ViewController连接起来吗?我不明白这个:X 我看到一种叫做“包装器”的东西,但我不记得在哪里了,我再也找不到了 这是我的代码: 1-我在共享/PCL项目中创建了一个接口C# 如何在窗体上使用组件?,c#,xamarin,cross-platform,shared,xamarin.forms,C#,Xamarin,Cross Platform,Shared,Xamarin.forms,我从Xamarin的组件商店得到了一个组件,Android和iOS都可以使用它 如何在共享/可移植项目(表单)上使用此组件? 或者我可以将表单内容与ViewController连接起来吗?我不明白这个:X 我看到一种叫做“包装器”的东西,但我不记得在哪里了,我再也找不到了 这是我的代码: 1-我在共享/PCL项目中创建了一个接口 namespace UsingDependencyService { public interface IActionTray { vo
namespace UsingDependencyService
{
public interface IActionTray
{
void ActoinTrayBiulder ();
}
}
public interface IActionPackAlert
{
void ShowAlert(string title, string text);
}
2-我实现了接口
[assembly: Xamarin.Forms.Dependency (typeof(ActionTray_iOS))]
namespace UsingDependencyService.iOS
{
public class ActionTray_iOS : IActionTray
{
UIActionTray leftTray = new UIActionTray (UIScreen.MainScreen.Bounds);
public ActionTray_iOS ()
{
}
public void ActoinTrayBiulder ()
{
leftTray.trayType = UIActionTrayType.Draggable;
leftTray.orientation = UIActionTrayOrientation.Left;
leftTray.tabLocation = UIActionTrayTabLocation.BottomOrRight;
leftTray.frameType = UIActionTrayFrameType.EdgeOnly;
leftTray.tabType = UIActionTrayTabType.IconAndTitle;
// Style tray
leftTray.appearance.background = UIColor.LightGray;
leftTray.appearance.frame = UIColor.DarkGray;
//leftTray.icon=UIActionImage.FromFile ("Images/icon_calendar.png");
leftTray.title = "Tab";
leftTray.CloseTray (false);
}
}
}
[assembly: Xamarin.Forms.Dependency (typeof (ActionPackAlert_iOS))]
public class ActionPackAlert_iOS : IActionPackAlert
{
public void ShowAlert(string title, string text)
{
UIActionAlert.ShowAlert (title, text);
}
}
3-我试图从共享/PCL项目中使用它:
public class MainPage : ContentPage
{
public MainPage ()
{
DependencyService.Get<IActionTray> ().ActoinTrayBiulder ();
}
}
}
DependencyService.Get<IActionPackAlert>().ShowAlert("Hello from Xamarin Forms", "This was called platform-specifically from the shared project");
public类主页:ContentPage
{
公共主页()
{
DependencyService.Get().actIntrayBiulder();
}
}
}
它被称为…但它没有出现在我的视图中。
我是怎么做到的?我相信你要找的是Xamarin的DependencyService() 您应该将组件添加到特定于平台的项目中,然后创建用于从表单项目访问组件的接口 下面是一些代码来解释这个过程 首先,在shared/PCL项目中创建一个接口
namespace UsingDependencyService
{
public interface IActionTray
{
void ActoinTrayBiulder ();
}
}
public interface IActionPackAlert
{
void ShowAlert(string title, string text);
}
在这个例子中,我们只做iOS。因此,在您的iOS项目中,您应该实现该接口
[assembly: Xamarin.Forms.Dependency (typeof(ActionTray_iOS))]
namespace UsingDependencyService.iOS
{
public class ActionTray_iOS : IActionTray
{
UIActionTray leftTray = new UIActionTray (UIScreen.MainScreen.Bounds);
public ActionTray_iOS ()
{
}
public void ActoinTrayBiulder ()
{
leftTray.trayType = UIActionTrayType.Draggable;
leftTray.orientation = UIActionTrayOrientation.Left;
leftTray.tabLocation = UIActionTrayTabLocation.BottomOrRight;
leftTray.frameType = UIActionTrayFrameType.EdgeOnly;
leftTray.tabType = UIActionTrayTabType.IconAndTitle;
// Style tray
leftTray.appearance.background = UIColor.LightGray;
leftTray.appearance.frame = UIColor.DarkGray;
//leftTray.icon=UIActionImage.FromFile ("Images/icon_calendar.png");
leftTray.title = "Tab";
leftTray.CloseTray (false);
}
}
}
[assembly: Xamarin.Forms.Dependency (typeof (ActionPackAlert_iOS))]
public class ActionPackAlert_iOS : IActionPackAlert
{
public void ShowAlert(string title, string text)
{
UIActionAlert.ShowAlert (title, text);
}
}
太好了。我们已经完成了特定于平台的实现。现在,让我们尝试从shared/PCL项目中使用它:
public class MainPage : ContentPage
{
public MainPage ()
{
DependencyService.Get<IActionTray> ().ActoinTrayBiulder ();
}
}
}
DependencyService.Get<IActionPackAlert>().ShowAlert("Hello from Xamarin Forms", "This was called platform-specifically from the shared project");
DependencyService.Get().showart(“来自Xamarin表单的您好”,“这是专门从共享项目中调用的平台”);
我怀疑您的问题在于您为UIActionTray设置的帧/边界,以及操作系统何时设置这些边界
尽管如此,我还是能够使用一些自定义渲染器将ActionTray与Xamarin.Forms(PCL)一起使用。以下是ActionTray的iOS自定义呈现程序示例:
在Xamarin.Forms PCL项目中,您至少需要以下内容:
public class ActionTrayView : View
{
public static BindableProperty TrayTypeProperty = BindableProperty.Create((ActionTrayView v) => v.TrayType, ActionTrayType.Draggable);
public ActionTrayType TrayType
{
get
{
return (ActionTrayType)GetValue(TrayTypeProperty);
}
set
{
SetValue(TrayTypeProperty, value);
}
}
public static BindableProperty OrientationProperty = BindableProperty.Create((ActionTrayView v) => v.Orientation, ActionTrayOrientation.Bottom);
public ActionTrayOrientation Orientation
{
get
{
return (ActionTrayOrientation)GetValue(OrientationProperty);
}
set
{
SetValue(OrientationProperty, value);
}
}
}
public enum ActionTrayType
{
/// <summary>
/// When the user taps the ActionTray's DragTab, the tray will snap between its openedPostion and closedPostion.
/// If the tray is open and the user taps anywhere within its content area, the tray will automatically close.
/// </summary>
AutoClosingPopup,
/// <summary>
/// The ActionTray can be dragged by its dragTab between the specified openedPosition and closedPosition
/// </summary>
Draggable,
/// <summary>
/// When the user taps the ActionTray's DragTab, the tray will snap between its openedPosition and closedPosition
/// </summary>
Popup
}
public enum ActionTrayOrientation
{
/// <summary>
/// The ActionTray is at the bottom of the screen
/// </summary>
Bottom,
/// <summary>
/// The ActionTray is on the left side of the screen
/// </summary>
Left,
/// <summary>
/// The ActionTray is on the ride side of the screen
/// </summary>
Right,
/// <summary>
/// The ActionTray is at the top of the screen
/// </summary>
Top
}
公共类ActionTrayView:视图
{
公共静态BindableProperty TrayTypeProperty=BindableProperty.Create((ActionTrayView v)=>v.TrayType,ActionTrayType.Dragable);
公共操作TrayType TrayType
{
得到
{
return(ActionTrayType)GetValue(TrayTypeProperty);
}
设置
{
SetValue(TrayTypeProperty,value);
}
}
public static BindableProperty OrientationProperty=BindableProperty.Create((ActionTrayView v)=>v.Orientation,ActionTrayOrientation.Bottom);
公共行动导向
{
得到
{
返回(ActionTrayorOrientation)GetValue(方向属性);
}
设置
{
SetValue(方向属性、值);
}
}
}
公共枚举ActionTrayType
{
///
///当用户轻触ActionTray的DragTab时,托盘将在其打开位置和关闭位置之间咬合。
///如果任务栏打开且用户轻触其内容区域内的任何位置,任务栏将自动关闭。
///
自动关闭弹出窗口,
///
///ActionTray可以通过其dragTab在指定的打开位置和关闭位置之间拖动
///
拖拉的,
///
///当用户点击ActionTray的DragTab时,托盘将在其打开位置和关闭位置之间咬合
///
弹出窗口
}
公共枚举ActionTrayorOrientation
{
///
///ActionTray位于屏幕底部
///
底部,
///
///ActionTray位于屏幕的左侧
///
左边
///
///ActionTray位于屏幕的行驶侧
///
正确的,
///
///ActionTray位于屏幕顶部
///
顶部
}
在.iOS项目中,您需要:
using System.Drawing;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using System.ComponentModel;
using **YourXamarinFormsPCLProjectReference**;
using **YourXamarinFormsPCLProjectReference**.iOS;
using UIKit;
using System;
using System.Collections.Generic;
using CoreGraphics;
using Appracatappra.ActionComponents.ActionTray;
[assembly: ExportRenderer(typeof(ActionTrayView), typeof(ActionTrayRendereriOS))]
namespace **YourXamarinFormsPCLProjectReference**.iOS
{
public class ActionTrayRendereriOS : ViewRenderer<ActionTrayView, UIActionTray>
{
protected override void OnElementChanged (ElementChangedEventArgs<ActionTrayView> e)
{
base.OnElementChanged(e);
if(e.OldElement == null)
{
SetNativeControl(new UIActionTray());
Control.trayType = GetTrayType(e.NewElement.TrayType);
Control.orientation = GetTrayOrientation(e.NewElement.Orientation);
}
}
public override void LayoutSubviews ()
{
base.LayoutSubviews();
Control.CloseTray(false);
}
private UIActionTrayType GetTrayType(ActionTrayType trayType)
{
switch(trayType)
{
case ActionTrayType.AutoClosingPopup:
{
return UIActionTrayType.AutoClosingPopup;
}
case ActionTrayType.Draggable:
{
return UIActionTrayType.Draggable;
}
case ActionTrayType.Popup:
{
return UIActionTrayType.Popup;
}
default:
{
return UIActionTrayType.Draggable;
}
}
}
private UIActionTrayOrientation GetTrayOrientation(ActionTrayOrientation orientation)
{
switch (orientation)
{
case ActionTrayOrientation.Bottom:
{
return UIActionTrayOrientation.Bottom;
}
case ActionTrayOrientation.Left:
{
return UIActionTrayOrientation.Left;
}
case ActionTrayOrientation.Right:
{
return UIActionTrayOrientation.Right;
}
case ActionTrayOrientation.Top:
{
return UIActionTrayOrientation.Top;
}
default:
{
return UIActionTrayOrientation.Bottom;
}
}
}
}
使用系统图;
使用Xamarin.Forms.Platform.iOS;
使用Xamarin.Forms;
使用系统组件模型;
使用**YourXamarinFormsPCLProjectReference**;
使用**YourXamarinFormsPCLProjectReference**.iOS;
使用UIKit;
使用制度;
使用System.Collections.Generic;
使用核心图形;
使用Appracatapra.ActionComponents.ActionTray;
[程序集:ExportRenderer(typeof(ActionTrayView)、typeof(ActionTrayRenderReios))]
命名空间**YourXamarinFormsPCLProjectReference**.iOS
{
公共类ActionTrayRendereriOS:ViewRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(e.OldElement==null)
{
SetNativeControl(新UIActionTray());
Control.trayType=GetTrayType(e.NewElement.trayType);
Control.orientation=GetTrayOrientation(例如,NewElement.orientation);
}
}
公共覆盖无效布局子视图()
{
base.LayoutSubviews();
控件。关闭托盘(错误);
}
私有UIActionTrayType GetTrayType(ActionTrayType trayType)
{
开关(trayType)
{
案例ActionTrayType.AutoClosingPopup:
{
返回UIActionTrayType.AutoClosingPopup;
}
案例ActionTrayType.Dragable:
{
返回UIActionTrayType.Draggable;
}
案例ActionTrayType.Popup:
{
返回UIActionTrayType.Popup;
}
违约:
{
返回UIActionTrayType.Draggable;
}
}
}
私有UIActionTrayorOrientation GetTrayOrientation(ActionTrayorOrientation方向)
{
开关(方向)
{
案例行动转移方向。底部:
{
返回UIActionTrayorOrientation.Bottom;
}
案例行动转移方向。左:
{
返回UIActionTrayorOrientation。左;
}
案例行动转移方向。右:
{
返回UIActionTrayorOrientation。对;
}
案例行动转移方向。顶部:
{
返回UIActionTrayorOrientation.Top;
}
诽谤