Xamarin PCL的Messaging PlugIn(Carel Lotz)?

问题描述:

所以我在我的可移植类库中有这个按钮,它应该直接将我创建的xml文件附加到邮件中并使用消息传递插件发送它。问题是.WithAttachment()在PCL中不受支持,所以我想问一下我是否可以使用DependencyService解决这个问题,如果可以的话,怎么样?Xamarin PCL的Messaging PlugIn(Carel Lotz)?

我可以直接从UWP类中返回.WithAttachment()(因为UWP是我的目标平台)?不会有冲突,因为我读过UWP中.WithAttachment()的重载是.WithAttachment(IStorageFile文件)。

private void Senden_Clicked(object sender, EventArgs e) 
     { 
      var emailMessenger = CrossMessaging.Current.EmailMessenger; 
      if (emailMessenger.CanSendEmail) 
      { 
       var email = new EmailMessageBuilder() 
        .To("[email protected]") 
        .Subject("Test") 
        .Body("Hello there!") 
        //.WithAttachment(String FilePath, string ContentType) overload showing in PCL 
        //.WithAttachment(IStorageFile file) overload for the UWP according to the documentation 
        .Build(); 
       emailMessenger.SendEmail(email); 
      } 

     } 

编辑:

我已经能够修改尼克周某的回答一点点能够通过按钮点击带附件发送电子邮件。我只是改变了这个和平的代码:

var picker = new Windows.Storage.Pickers.FileOpenPicker 
    { 
     ViewMode = Windows.Storage.Pickers.PickerViewMode.List, 
     SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary 
    }; 
    picker.FileTypeFilter.Add("*"); 

这样:

StorageFolder sf = ApplicationData.Current.LocalFolder; 
      var file = await sf.GetFileAsync("daten.xml"); 

当然,你需要创建应用程序的本地文件夹,而不是文件库中的文件。

问题是.WithAttachment()在PCL中不受支持,所以我想问问是否可以使用DependencyService解决这个问题,如果是这样的话,怎么样?

当然,您可以使用DependencyService来实现用附件发送电子邮件。但是你可以创建两个类似代码的interface

SendEmail接口

public interface IMessageEmail 
    { 
     void SendEmailMehod(string address, string subject, string body, StorageFile attactment = null); 
    } 

在UWP项目IMessageEmail实施。

public void SendEmailMehod(string address, string subject, string body, StorageFile attactment = null) 
{ 
    var emailMessenger = CrossMessaging.Current.EmailMessenger; 
    if (emailMessenger.CanSendEmail) 
    { 
     if (attactment != null) 
     { 
      var email = new EmailMessageBuilder() 
     .To(address) 
     .Subject(subject) 
     .Body(body) 
     .WithAttachment(attactment) 
     .Build(); 
      emailMessenger.SendEmail(email); 
     } 
     else 
     { 
      var email = new EmailMessageBuilder() 
     .To(address) 
     .Subject(subject) 
     .Body(body) 
     .Build(); 
      emailMessenger.SendEmail(email); 
     } 
    } 
} 

正如你可以看到.WithAttachment(attactment)参数是IStorageFile。所以你需要传递一个文件给方法。因此,您可以创建另一个DependencyService

IFilePicker接口

public interface IFilePicker 
    { 
     Task<StorageFile> getFileAsync(); 
    } 

在UWP项目IMessageEmail实施。

public async Task<StorageFile> getFileAsync() 
{ 
    var picker = new Windows.Storage.Pickers.FileOpenPicker 
    { 
     ViewMode = Windows.Storage.Pickers.PickerViewMode.List, 
     SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary 
    }; 
    picker.FileTypeFilter.Add("*"); 

    var file = await picker.PickSingleFileAsync(); 

    if (file != null) 
    { 
     return file; 
    } 

    return null; 
} 

您可以尝试project我已上传到github。

+0

非常感谢你,先生!你知道是否有办法自动获取文件或将默认位置设置为DocumentsLibrary?由于'SuggestedStartLocation'没有将其设置为DocumentsLibrary(根据文档,它只是记住用户选择的最后一个文件夹或类似的东西)。 – RoloffM

+0

@Nick好吧,我现在明白了!而你,先生,你真正的MVP!非常感谢你 RoloffM