3

我有 2 页,在第一页中,当我单击列表视图中的一个项目时,它应该导航到第 2 页,并且在第 2 页中出现雪粒子。

之后,当我单击后退按钮时,它像往常一样转到第一页,当我单击导航到第二页的任何列表视图项目时,雪粒子消失了,它只显示黑屏。

以下是代码:

对于雪地渲染,我有两个课程

第一

public class SnowScene : CCScene
{
    CCParticleSnow snow;

    public SnowScene(CCGameView gameView) : base(gameView)
    {
        var layer = new CCLayer();
        layer.Color=new CCColor3B(new CCColor4B(20,30,50,10));

        snow = new CCParticleSnow(new CCPoint(490, 20));
        snow.Position = new CCPoint(490 / 2, 800 + 1);
        snow.StartColor = new CCColor4F(CCColor4B.White);
        snow.EndColor = new CCColor4F(CCColor4B.Red);
        snow.StartSize = 15f;
        snow.StartSizeVar = 2f;
        snow.EndSize = 8f;
        snow.EndSizeVar = 1f;
        snow.Speed = 5f;
        snow.Gravity = new CCPoint(0.5f, -2);
        snow.EmissionRate = 3.5f;
        snow.Life = 50f;

        layer.AddChild(snow);
        this.AddLayer(layer);
    }
}

第二个

public class SnowyBackground : ContentView
{
    SnowScene overallScene;

    public SnowyBackground()
    {
        var sharpView = new CocosSharpView
        {
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand
        };

        sharpView.ViewCreated += (sender, e) =>
        {
            var ccGView = sender as CCGameView;

            if (ccGView != null)
            {
                ccGView.DesignResolution = new CCSizeI(490, 800);
                overallScene = new SnowScene(ccGView);
                ccGView.RunWithScene(overallScene);
            }
        };

        Content = sharpView;
    }

}

现在,在第二页的 Xaml 中, 当我单击第一页(page1)中的列表视图项时,如下所示:Listview 项的事件处理程序单击第一页-

async void Handle_ItemSelected(object sender, Xamarin.Forms.SelectedItemChangedEventArgs e)
{
    try
    {
        await Navigation.PushModalAsync(new Page2(),false);
    }
    catch(Exception ex)
    { }
}

第二页——

<?xml version="1.0" encoding="utf-8"?>
<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:local="clr-namespace:Particle" 
    x:Class="Particle.ParticlePage">

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <local:SnowyBackgroundView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Grid.RowSpan="2" />

</Grid>

它导航到第 2 页,并成功显示雪渲染。但是当我重复相同的步骤时......我的意思是如果我点击第 2 页中的后退按钮,那么它会转到第 1 页,当我再次单击 lictview 项目时,它会转到第二页,但它不显示雪渲染,只是一个黑色屏幕。

4

0 回答 0