【问题标题】:How to show image from shared folder on xamarin forms(android)?如何在 xamarin 表单(android)上显示共享文件夹中的图像?
【发布时间】:2020-11-16 13:04:14
【问题描述】:

我在 xamarin 表单上显示图像时遇到问题。图像存储在我们网络上的共享文件夹中。我正在使用 Sharpcifs。

以下是相关视图模型的代码:

ImageSource ProductImage { get; set; }


    public ProductPositionsViewModel()
    {
        Title = "Produit/Pos";
        Items = new ObservableCollection<ListItem>();

        SharpCifs.Config.SetProperty("jcifs.smb.client.lport", "8080");

        //Get Auth
        var auth = new NtlmPasswordAuthentication("Domain","User","Password");

        //Get target's SmbFile.
        var smb = new SmbFile("smb://file/products/ABY/ABYBAG167.jpg", auth);

        Console.WriteLine($"exists? {smb.Exists()}");
        // It returns true, I can confirm.

        //Get readable stream.
        var readStream = smb.GetInputStream();

        
        

        //Create reading buffer.
        var memStream = new MemoryStream();

        //Get bytes.
        ((Stream)readStream).CopyTo(memStream);

        using (MemoryStream memorystream = memStream)
        {

            ProductImage = ImageSource.FromStream(() => memorystream);
        }

        

        //Dispose readable stream.
        readStream.Dispose();

        LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());
    }

我用泛型替换了域、用户名和密码。我确实使用了正确的域名、用户名和密码。 并在 xaml 中:

<?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:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="Picking.Views.ProductPositionsPage"
         Title="{Binding Title}">


<ContentPage.Content>


    <Grid RowSpacing="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="70"/>
        </Grid.RowDefinitions>

        <Image Grid.Row="0" Source="{Binding ProductImage}"/>


    </Grid>




</ContentPage.Content>
</ContentPage>

【问题讨论】:

    标签: c# image xaml xamarin.forms


    【解决方案1】:

    如果你想在运行时设置Image的Source,别忘了在你的ViewModel中实现INotifyPropertyChanged接口

    public class ProductPositionsViewModel: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
    
      
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    
        ImageSource productImage;
    
        ImageSource ProductImage { 
            
            get
            {
                return productImage;
            }
    
            set
            {
                if(productImage!=value)
                {
                    productImage = value;
                    OnPropertyChanged("ProductImage");
                }
            }
        }
    
    
        public ProductPositionsViewModel()
        {
    
            //...
          
            using (MemoryStream memorystream = memStream)
            {
    
                ProductImage = ImageSource.FromStream(() => {
                    
                     //...  // whatever you need to do to create your stream
    
               });
            }
        }
    
    }
    

    【讨论】:

    • 我确实忘记通知属性改变了,虽然我已经添加了接口。我添加了您的代码,但它仍然没有显示图像。现在想来,难道是我要关闭流了?它需要流来访问图像,还是只在第一次加载它?
    猜你喜欢
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    相关资源
    最近更新 更多