求无XAML的Xamarin.Forms示例代码及闪屏页切换至动态页实现方案
纯代码实现Xamarin.Forms闪屏页到应用页的切换示例
核心实现逻辑
- 应用启动时直接显示闪屏页,避免UI阻塞
- 异步执行GPS坐标获取、应用页动态UI构建等耗时操作
- 操作完成后通过主线程安全切换主页面,替换闪屏页
完整代码示例
1. 应用入口类(App.cs)
using Xamarin.Forms; using System.Threading.Tasks; namespace CodeOnlyXamarinApp { public class App : Application { public App() { // 启动时加载闪屏页 MainPage = new SplashPage(); } protected override async void OnStart() { base.OnStart(); await LoadMainPageAsync(); } private async Task LoadMainPageAsync() { try { // 获取用户GPS坐标(实际项目替换为Xamarin.Essentials的Geolocation API) var gpsCoordinates = await GetUserGpsCoordinatesAsync(); // 异步构建应用页 var mainAppPage = new MainAppPage(gpsCoordinates); // 主线程切换页面,保证UI操作安全 Device.BeginInvokeOnMainThread(() => { MainPage = mainAppPage; }); } catch (System.Exception ex) { // 加载失败时显示错误页 Device.BeginInvokeOnMainThread(() => { MainPage = new ContentPage { Content = new Label { Text = $"加载失败: {ex.Message}", HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center } }; }); } } // 模拟GPS定位耗时操作 private async Task<(double Latitude, double Longitude)> GetUserGpsCoordinatesAsync() { await Task.Delay(2000); return (39.9042, 116.4074); // 示例坐标:北京 } } }
2. 闪屏页(SplashPage.cs)
using Xamarin.Forms; namespace CodeOnlyXamarinApp { public class SplashPage : ContentPage { public SplashPage() { var splashLayout = new AbsoluteLayout(); // 添加加载指示器 var loadingIndicator = new ActivityIndicator { IsRunning = true, Color = Color.Blue }; AbsoluteLayout.SetLayoutFlags(loadingIndicator, AbsoluteLayoutFlags.PositionProportional); AbsoluteLayout.SetLayoutBounds(loadingIndicator, new Rectangle(0.5, 0.5, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize)); // 添加加载提示文本 var titleLabel = new Label { Text = "加载中...", FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)), TextColor = Color.Gray }; AbsoluteLayout.SetLayoutFlags(titleLabel, AbsoluteLayoutFlags.PositionProportional); AbsoluteLayout.SetLayoutBounds(titleLabel, new Rectangle(0.5, 0.6, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize)); splashLayout.Children.Add(loadingIndicator); splashLayout.Children.Add(titleLabel); Content = splashLayout; BackgroundColor = Color.White; } } }
3. 动态构建的应用页(MainAppPage.cs)
using Xamarin.Forms; namespace CodeOnlyXamarinApp { public class MainAppPage : ContentPage { public MainAppPage((double Latitude, double Longitude) gpsCoords) { var mainLayout = new AbsoluteLayout(); // 根据GPS坐标生成动态文本 var locationLabel = new Label { Text = $"当前坐标:\n纬度: {gpsCoords.Latitude:F4}\n经度: {gpsCoords.Longitude:F4}", FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)), TextColor = Color.Black, HorizontalTextAlignment = TextAlignment.Center }; AbsoluteLayout.SetLayoutFlags(locationLabel, AbsoluteLayoutFlags.PositionProportional); AbsoluteLayout.SetLayoutBounds(locationLabel, new Rectangle(0.5, 0.5, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize)); // 添加刷新按钮 var refreshButton = new Button { Text = "刷新坐标", BackgroundColor = Color.Blue, TextColor = Color.White }; refreshButton.Clicked += async (s, e) => { var newCoords = await ((App)App.Current).GetUserGpsCoordinatesAsync(); locationLabel.Text = $"当前坐标:\n纬度: {newCoords.Latitude:F4}\n经度: {newCoords.Longitude:F4}"; }; AbsoluteLayout.SetLayoutFlags(refreshButton, AbsoluteLayoutFlags.PositionProportional); AbsoluteLayout.SetLayoutBounds(refreshButton, new Rectangle(0.5, 0.7, 150, 40)); mainLayout.Children.Add(locationLabel); mainLayout.Children.Add(refreshButton); Content = mainLayout; BackgroundColor = Color.LightGray; Title = "应用主页"; } } }
关键说明
- 异步非阻塞:耗时操作放在异步方法中,保证闪屏页正常显示加载状态
- UI线程安全:页面切换必须通过
Device.BeginInvokeOnMainThread执行,符合Xamarin.Forms的UI操作规则 - 纯代码构建:所有UI元素均通过C#代码创建,完全适配纯代码项目的迁移需求
- 异常兜底:添加异常捕获逻辑,避免加载失败导致应用无响应
内容的提问来源于stack exchange,提问作者DumbestGuyOnSaturn
相关产品推荐
相关产品推荐

