【问题标题】:How to open file .doc use "Word" app on Xamarin Forms?如何在 Xamarin Forms 上使用“Word”应用程序打开文件 .doc?
【发布时间】:2018-11-27 00:29:28
【问题描述】:

使用 Xamarin Forms 和在 iOS 上,通过 UrL 打开文件时如何检查和打开Word app? 参考:https://docs.microsoft.com/en-us/office/client-developer/integration/integrate-with-office-from-ios-applications

这是我的代码,它不起作用:

Device.OpenUri(new Uri("ms-word:ofe|u|https://calibre-ebook.com/downloads/demos/demo.docx"));

请帮帮我! 谢谢!

【问题讨论】:

  • 你需要一个依赖服务,它不是 xamarin 形式的东西!
  • 我想在 iOS 上写代码。您可以提供解决方案@G.hakim
  • 我猜this 指南这里有必要的信息。
  • 我只想要外部应用程序(“Word”应用程序itunes.apple.com/us/app/microsoft-word/id586447913?mt=8)上的视图文件.doc。可能是你的解决方案不适合我。
  • 我发现答案与您的答案相同 stackoverflow.com/questions/51494932/… 但您不想要“编辑”文件,您只想要使用 Word 应用程序“查看”文件 .docx?应用程序。

标签: xamarin xamarin.forms xamarin.ios


【解决方案1】:

您在此处询问的内容可以在 iOS 中使用简单的 WebView 来完成:

首先,创建一个允许您选择文件 uri 的自定义 WebView 类:

public class CustomWebView : WebView
{
    public static readonly BindableProperty UriProperty = BindableProperty.Create(propertyName: "Uri",
            returnType: typeof(string),
            declaringType: typeof(CustomWebView),
            defaultValue: default(string));

    public string Uri
    {
        get { return (string)GetValue(UriProperty); }
        set { SetValue(UriProperty, value); }
    }

}

然后在 ios 中为它创建一个渲染器并执行以下操作:

  [assembly: ExportRenderer (typeof(CustomWebView), typeof(CustomWebViewRenderer))]
  namespace DisplayPDF.iOS
 {
   public class CustomWebViewRenderer : ViewRenderer<CustomWebView, UIWebView>
  {
    protected override void OnElementChanged (ElementChangedEventArgs<CustomWebView> e)
    {
        base.OnElementChanged (e);

        if (Control == null) {
            SetNativeControl (new UIWebView ());
        }
        if (e.OldElement != null) {
            // Cleanup
        }
        if (e.NewElement != null) {
            var customWebView = Element as CustomWebView;
            string fileName = Path.Combine (NSBundle.MainBundle.BundlePath, string.Format ("Content/{0}", WebUtility.UrlEncode (customWebView.Uri)));
            Control.LoadRequest (new NSUrlRequest (new NSUrl (fileName, false)));
            Control.ScalesPageToFit = true;
          }
       }
   }
}

然后像这样使用自定义控件:

 <local:CustomWebView Uri="FooPath" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" /> 

其中 FooPath 是 doc 文件的路径。

【讨论】:

  • 试过这个代码,但是 pdf/word 文件没有打开。显示NSURLConnection finished with error - code -110
【解决方案2】:

我的项目有解决方案,我试试看:

如果你只在iOS上打开.docx文件,你可以在iOS的Share Code处写代码:

  1. 检查用户的设备是否有设置应用程序(Word、Excel、PP 等...)

    public static bool HasSetupAppDocument(string extension)
    {
        if (string.IsNullOrEmpty(extension))
            return false;
    
        // Device has setup app?
        var result = UIApplication.SharedApplication.CanOpenUrl(NSUrl.FromString($"{extension}"));
        return result;
    }
    

(例如:extensionms-word:ms-excel:ms-excel:

参考https://docs.microsoft.com/en-us/office/client-developer/integration/integrate-with-office-from-ios-applications#office-protocols)

备注:将来源添加到Info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
     <string>ms-word</string>
     <string>ms-excel</string>
     <string>ms-powerpoint</string>
</array>
  1. 在 Class Dependencies 中,使用 URL 打开文件:

    Device.OpenUri(new Uri($"{convertExtension}{url}"));

注意:urlOnedrive 上的链接文件共享,并确保帐户 Onedrive 与帐户登录 Word 应用程序相同(如果您已设置安全性)。

如果文件的模式为Read-only,应用程序Word 将打开模式为Read-only 的文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-15
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多