如何在MAUI页面中使用多个EvergineView?
同一MAUI页面中使用多个EvergineView抛出异常问题
在MAUI应用的同一页面中使用多个EvergineView时持续抛出异常,已基于Evergine官方EverSneaks项目做测试验证,相关修改及异常信息如下:
XAML修改(SneakersDetailsView.xaml)
<Grid> <Grid.RowDefinitions> <RowDefinition Height="60*" /> <RowDefinition Height="40*" /> <RowDefinition Height="60" /> </Grid.RowDefinitions> <Grid Grid.Row="0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <evergine:EvergineView x:Name="evergineView0" Grid.Column="0"/> <Image Grid.Column="0" Source="black_heart.png" Style="{StaticResource HeartStyle}" /> <evergine:EvergineView x:Name="evergineView1" Grid.Column="1"/> <Image Grid.Column="1" Source="black_heart.png" Style="{StaticResource HeartStyle}" /> </Grid>
代码后台修改(SneakersDetailsView.xaml.cs)
public partial class SneakersDetailsView : ContentPage { public SneakersDetailsView() { InitializeComponent(); this.evergineView0.Application = new MyApplication(); this.evergineView1.Application = new MyApplication(); this.BindingContext = new SneakersDetailsViewModel(this.evergineView0, this.evergineView1); } // Unchanged methods }
ViewModel修改(SneakersDetailsViewModel.cs)
public class SneakersDetailsViewModel { public ICommand ColorCommand { get; set; } private EvergineView evergineView0; private EvergineView evergineView1; private ControllerService controllerService0; private ControllerService controllerService1; public SneakersDetailsViewModel(EvergineView evergineView0, EvergineView evergineView1) { this.evergineView0 = evergineView0; this.controllerService0 = this.evergineView0.Application.Container.Resolve<ControllerService>(); this.evergineView1 = evergineView1; this.controllerService1 = this.evergineView1.Application.Container.Resolve<ControllerService>(); this.ColorCommand = new Command<string>( (color) => { switch (color) { case "red": this.controllerService0.Color = ControllerService.SneakerColor.Red; this.controllerService1.Color = ControllerService.SneakerColor.Red; break; case "orange": this.controllerService0.Color = ControllerService.SneakerColor.Orange; this.controllerService1.Color = ControllerService.SneakerColor.Orange; break; case "blue": this.controllerService0.Color = ControllerService.SneakerColor.Blue; this.controllerService1.Color = ControllerService.SneakerColor.Blue; break; case "gray": default: this.controllerService0.Color = ControllerService.SneakerColor.Gray; this.controllerService1.Color = ControllerService.SneakerColor.Gray; break; } }); } }
异常信息
异常表现因Evergine版本不同而有差异:
- 使用2024.10.24.7版本时,在
MyApplication.Initialize()中抛出Evergine.Framwork.Exceptions.BindingException: [GraphicsPresenter].graphicsContext Failed resolving [GraphicsContext] - 使用2024.6.28.10版本时,在
ControllerService.Start()中抛出System.ArgumentNullException: Value cannot be null. Arg_ParamName_Name
已知正常场景
当应用中多页面各使用一个EvergineView时可正常运行,且明确不能使用静态字段Application.Current,因为每次实例化新EvergineView时该字段会被覆盖,导致不可预测的行为。
内容的提问来源于stack exchange,提问作者Mathieu Flaust
相关产品推荐
相关产品推荐

