You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发支持双显示屏的Xamarin.Forms应用技术方案咨询

Great question! Dual-screen support in Xamarin.Forms does require some platform-specific work since the framework doesn’t have out-of-the-box APIs for this scenario. Here’s a practical, step-by-step approach I’ve used in similar projects:

Approach Overview

The core idea is to:

  1. Detect when an external display connects using native platform APIs
  2. Render your Xamarin.Forms summary page in a dedicated container (activity/window) on the secondary display
  3. Sync data between the main and secondary screens using shared view models or event messaging

Android Implementation

Android provides a DisplayManager to monitor connected displays. Here’s how to use it:

  1. Register a display listener in your MainActivity to detect external display connections:
public class MainActivity : FormsAppCompatActivity
{
    private DisplayManager _displayManager;
    private DisplayListener _displayListener;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        _displayManager = (DisplayManager)GetSystemService(DisplayService);
        _displayListener = new DisplayListener(this);
    }

    protected override void OnResume()
    {
        base.OnResume();
        _displayManager.RegisterDisplayListener(_displayListener, null);
    }

    protected override void OnPause()
    {
        base.OnPause();
        _displayManager.UnregisterDisplayListener(_displayListener);
    }
}

public class DisplayListener : Java.Lang.Object, DisplayManager.IDisplayListener
{
    private readonly MainActivity _activity;

    public DisplayListener(MainActivity activity)
    {
        _activity = activity;
    }

    public void OnDisplayAdded(int displayId)
    {
        var display = _activity._displayManager.GetDisplay(displayId);
        if (display != null && display.Type == DisplayType.External)
        {
            // Launch a dedicated activity for the secondary display
            var intent = new Intent(_activity, typeof(SecondaryDisplayActivity));
            intent.AddFlags(ActivityFlags.NewTask);
            _activity.StartActivity(intent);
        }
    }

    public void OnDisplayRemoved(int displayId)
    {
        // Clean up resources when the display disconnects
    }

    public void OnDisplayChanged(int displayId)
    {
        // Handle display configuration updates
    }
}
  1. Create a secondary activity to host your summary page:
[Activity(Label = "SecondaryDisplay")]
public class SecondaryDisplayActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Convert your Forms summary page to an Android view
        var summaryPage = new SummaryDataPage();
        var renderer = summaryPage.CreateRenderer(this);
        SetContentView(renderer.View);
    }
}

iOS Implementation

iOS uses UIScreen notifications to detect external displays. Here’s the setup:

  1. Listen for screen connection events in your AppDelegate:
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    private UIWindow _secondaryWindow;

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();
        LoadApplication(new App());

        // Register for display connection/disconnection events
        NSNotificationCenter.DefaultCenter.AddObserver(UIScreen.DidConnectNotification, OnScreenConnected);
        NSNotificationCenter.DefaultCenter.AddObserver(UIScreen.DidDisconnectNotification, OnScreenDisconnected);

        return base.FinishedLaunching(app, options);
    }

    private void OnScreenConnected(NSNotification notification)
    {
        var externalScreen = notification.Object as UIScreen;
        if (externalScreen == null) return;

        // Create a window for the external display and load your summary page
        _secondaryWindow = new UIWindow(externalScreen.Bounds)
        {
            Screen = externalScreen,
            RootViewController = new SummaryDataPage().CreateViewController()
        };
        _secondaryWindow.MakeKeyAndVisible();
    }

    private void OnScreenDisconnected(NSNotification notification)
    {
        _secondaryWindow?.Dispose();
        _secondaryWindow = null;
    }
}

Forms Layer Integration

To keep your main and secondary screens in sync:

  • Use a shared ViewModel instance across both pages. This ensures summary data updates automatically when the main screen’s data changes:
// In your App class
public static SharedDataViewModel SharedViewModel { get; } = new SharedDataViewModel();

// In MainPage.xaml.cs
BindingContext = App.SharedViewModel;

// In SummaryDataPage.xaml.cs
BindingContext = App.SharedViewModel;
  • For one-time events (like user actions), use MessagingCenter or an event aggregator to communicate between pages.

Testing Tips

  • On Android: Use the emulator’s Extended Controls to simulate an external display.
  • On iOS: Use the simulator’s Hardware > External Displays menu to test different screen sizes.

This approach should get you up and running with dual-screen support. Looking forward to seeing your sample app once you’ve got it working—it’ll definitely be a great resource for the Xamarin.Forms community!

内容的提问来源于stack exchange,提问作者Hristo Alexsiev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:24:56