son*_*onu 2 c# cross-platform orientation xamarin xamarin.forms
如何在Xamarin.Forms
跨平台的特定页面上设置内容页面方向或屏幕方向.
我已经定了ScreenOrientation = ScreenOrientation
.
在所有平台属性的肖像,但我想在两个变化中显示一些页面显示纵向和横向,所以如何在`xamarin.forms中设置页面.
设置屏幕方向不是设备明智但在页面上设置一些页面显示在横向和一些页面显示在肖像中的xamarin形式跨平台
您可以通过DependencyService
为特定平台创建依赖关系来实现此目的.
试着这样做:
接口
public interface IOrientationService
{
void Landscape();
void Portrait();
}
Run Code Online (Sandbox Code Playgroud)
对于Android:
[assembly: Xamarin.Forms.Dependency(typeof(OrientationService))]
namespace Orientation
{
public class OrientationService: IOrientationService
{
public void Landscape()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Landscape;
}
public void portrait()
{
((Activity)Forms.Context).RequestedOrientation = ScreenOrientation.Portrait;
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于iOS:
[assembly: Xamarin.Forms.Dependency(typeof(OrientationService))]
namespace Orientation
{
public class OrientationService: IOrientationService
{
public void Landscape()
{
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.LandscapeLeft), new NSString("orientation"));
}
public void Portrait()
{
UIDevice.CurrentDevice.SetValueForKey(new NSNumber((int)UIInterfaceOrientation.Portrait), new NSString("orientation"));
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样使用它
DependencyService.Get<IOrientationService>().Landscape();
DependencyService.Get<IOrientationService>().Portrait();
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!
这就是我在 Android 中的做法。您必须将代码添加到MainActivity.cs
[Activity(...., ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
否则,如果用户转动设备,您的应用程序将引发异常。
然后添加这个:
//To check if device is allowed to rotate
private bool _allowLandscape;
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
private bool _allowLandscape;
protected override void OnCreate(Bundle bundle)
{
switch (Device.Idiom)
{
case TargetIdiom.Phone:
RequestedOrientation = ScreenOrientation.Portrait;
break;
case TargetIdiom.Tablet:
RequestedOrientation = ScreenOrientation.Landscape;
break;
}
base.OnCreate(bundle);
//Setup additional stuff that you need
//Calls from the view that should rotate
MessagingCenter.Subscribe<GraphicsView>(this, "graphic", sender =>
{
_allowLandscape = true;
OnConfigurationChanged(new Configuration());
});
//When the page is closed this is called
MessagingCenter.Subscribe<GraphicsView>(this, "return", sender =>
{
_allowLandscape = false;
OnConfigurationChanged(new Configuration());
});
LoadApplication(new App());
}
public override void OnConfigurationChanged(Configuration newConfig)
{
base.OnConfigurationChanged(newConfig);
switch (newConfig.Orientation)
{
case Orientation.Landscape:
switch (Device.Idiom)
{
case TargetIdiom.Phone:
if (!_allowLandscape)
{
LockRotation(Orientation.Portrait);
}
break;
case TargetIdiom.Tablet:
LockRotation(Orientation.Landscape);
break;
}
break;
case Orientation.Portrait:
switch (Device.Idiom)
{
case TargetIdiom.Phone:
if (!_allowLandscape)
{
LockRotation(Orientation.Portrait);
}
break;
case TargetIdiom.Tablet:
LockRotation(Orientation.Landscape);
break;
}
break;
case Orientation.Undefined:
if (Device.Idiom == TargetIdiom.Phone && _allowLandscape)
{
LockRotation(Orientation.Landscape);
}
else if (Device.Idiom == TargetIdiom.Phone && !_allowLandscape)
{
LockRotation(Orientation.Portrait);
}
break;
}
}
private void LockRotation(Orientation orientation)
{
switch (orientation)
{
case Orientation.Portrait:
RequestedOrientation = ScreenOrientation.Portrait;
break;
case Orientation.Landscape:
RequestedOrientation = ScreenOrientation.Landscape;
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望这对你有用。
归档时间: |
|
查看次数: |
3101 次 |
最近记录: |