通过Xamarin表单中的c#脚本动态更改图像

时间:2018-02-28 15:12:57

标签: image xamarin xamarin.forms

我有一个跨平台的应用程序,当用户在一个按钮中显示时,我需要显示一组图像,所以,我把这些图像文件命名为" img000.png"到" img029.png"在PCL解决方案的一个文件夹中,将这些图像作为" EmbeddedResource",之后,我用所有这些图像填​​充列表,并且它现在工作正常,即图像显示为我想要的,但是当我点击按钮显示列表中的下一个图像,图片不会转到下一个。

我有这个:

//...
public class ImageSetPage : BasePage
// BasePage encapsule a ContentPage...
{
    private string directory = "MyApp.Assets.";
    private int idx = 0;
    protected StackLayout _mainLayout;
    protected StackLayout _buttonStack;
    protected Image _btnPrevI;
    protected Label _displayName; 
    protected Image _btnNestI;
    protected Image _image;
    protected List<Image> _Images;

    public ImageSetPage()
    {
        this._Images = getImages();
        var prevI_Tap = new TapGestureRecognizer();
        prevI_Tap.Tapped += (s, e) =>
        {
            onClick("pvi");
        };
        var nextI_Tap = new TapGestureRecognizer();
        nextI_Tap.Tapped += (s, e) =>
        {
            onClick("nti");
        };

        /// begin layout
        base.Title = "set of Images";

        this._mainLayout = new StackLayout()
        {
            Orientation = StackOrientation.Vertical,
            BackgroundColor = Color.Black,
            Padding = new Thickness(0, 10, 0, 0)
        };

        this._btnPrevI = new Image()
        {
            Aspect = Aspect.AspectFill,
            Source = ImageSource.FromResource(directory+"prevbtn.png")
        };
         _btnPrevI.GestureRecognizers.Add(prevI_Tap);

        this._displayName = new Label()
        {
            Style = Device.Styles.SubtitleStyle,
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            VerticalOptions = LayoutOptions.CenterAndExpand,
            TextColor = Color.White,
       };

       this._btnNextI = new Image()
       {
            Aspect = Aspect.AspectFill,
            Source = ImageSource.FromResource(directory + "nextbtn.png")
        };
        _btnNextI.GestureRecognizers.Add(nextI_Tap);
        this._buttonStack.Children.Add(_btnPrevI);
        this._buttonStack.Children.Add(_displayName);
        this._buttonStack.Children.Add(_btnNextI);
        this._mainLayout.Children.Add(_buttonStack);
        FillImage(idx);
        this._mainLayout.Children.Add(this._image);
        this.Content = this._mainLayout;
    }

    private void FillImage(int i)
    {
        this._displayName.Text = "Image n# " + FillWithZeroes(i);
        // [EDIT 1] cemented these lines ...
        // this._image = null;
        // mi = _images[i];
        // this._image = mi;
        // [EDIT 2] the new try
        string f = directory + "imgs.img0" + FillWithZeroes(i) + ".png";
        this._image = new Image() {
            VerticalOptions = LayoutOptions.Center,
            Soruce = ImageSource.FromResource(f)
        };
        // In this way the image show and don't change when click
        // this Fking* code is a big S_OF_THE_B*
        // The Xamarin and the C# is brothers of this FKing* code
    }

    private void onClick(string v)
    {
        string vw = v;
        if (vw.Equals("pvi")) idx--;
        if (vw.Equals("nti")) idx++;
        if (idx <=  0) idx = 29;
        if (idx >= 29) idx = 0;
        FillImage(idx);
        vw = "";
    }

    private string FillWithZeroes(int v)
    {
        string s = v.ToString();
        string r = "";
        if (s.Length == 1) { r = "0" + s; } else { r = s; }
        return r;
    }

    // to fill a list of Images with files in a PCL folder
    private List<Image> getImages() 
    {
        string directory = "MyApp.Assets.";
        List<Image> imgCards = new List<Image>();
        int c = 0;
        for (c = 0; c < 30;c++) {
            string f = directory + "imgs.img0" + FillWithZeroes(c) + ".png";
            Image img = new Image();
            img.Source = ImageSource.FromResource(f);
            imgCards.Add(img);
        }            
        return imgCards;
    }
    // ...
}

但是图像不会改变,即改变,就像我在调试中看到的那样,但是当我点击按钮时,不会在布局中显示。 也许我做错了。 有人可以帮助我吗?

提前致谢

0 个答案:

没有答案