2017-03-03 58 views
0

所以我在我的可移植類庫中有這個按鈕,它應該直接將我創建的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"); 

當然,你需要創建應用程序的本地文件夾,而不是文件庫中的文件。

回答

1

問題是.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!非常感謝你<3 – RoloffM