想要在谷歌搜索

时间:2012-05-25 02:28:42

标签: c# windows-phone-7

在Windows Phone7的网络浏览器应用中,如果用户只在UrlTExtBox中输入“bing.com”,则UrlTextBox会自动填充“http:// www。”< / strong>以下代码显示。同时,如果用户只是在没有“。com”的情况下输入单词(如技术或项目玻璃),则UrlTextBox会再次自动填充“http://” 。但我需要,如果只有单词,那么它应该搜索谷歌或必应。 anybode可以帮助我吗?提前感谢您的辛勤工作!

 private void UrlTextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            Uri url;
            if (Uri.TryCreate(UrlTextBox.Text, UriKind.Absolute, out url))
            {
                this.urls[this.currentIndex] = UrlTextBox.Text;
                this.browsers[this.currentIndex].Navigate(url);
                navigationcancelled = false;
                this.browsers[this.currentIndex].Focus();
            }
            else
            {
                Navigate(UrlTextBox.Text);
            }
        }
    }

 private void Navigate(String address)
    {
        if (String.IsNullOrEmpty(address)) return;
        if (address.Equals("about:blank")) return;
        if (!address.StartsWith("http://") &&
            !address.StartsWith("https://"))
        {
            address = "http://" + address;
        }
        try
        {
            browsers[this.currentIndex].Navigate(new Uri(address));
        }
        catch (System.UriFormatException)
        {
            return;
        }
    }

4 个答案:

答案 0 :(得分:0)

你不能用.拆分它,然后如果数组计数是1你知道它是一个单词并且应该添加.com,否则你应该把它当作正确的链接。

答案 1 :(得分:0)

在您的导航功能中检查它是否是框中的一个单词,如果是,请检查它是否以.com或.net等结尾..如果是,请添加http并发送,如果不将其附加到谷歌搜索网址。

答案 2 :(得分:0)

这是一个简单的控制台应用,它展示了您应该寻找的逻辑:

namespace SO10747736
{
    using System;

    internal enum TestResult
    {
        TryCreatePass,
        ValidWithPrefix,
        Search,
        DoNothing,
        Unknown
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            Test("http://example.com", TestResult.TryCreatePass);
            Test("https://example.com", TestResult.TryCreatePass);
            Test("https://example", TestResult.TryCreatePass);
            Test("example.com", TestResult.ValidWithPrefix);
            Test("example", TestResult.Search);
            Test("another example", TestResult.Search);
            Test(null, TestResult.DoNothing);
            Test(string.Empty, TestResult.DoNothing);
            Test("  ", TestResult.DoNothing);
            Test("about:blank", TestResult.DoNothing);

            Console.ReadKey(true);
        }

        private static void Test(string toTest, TestResult expectedResult)
        {
            var result = SimulateEnterkeyPressed(toTest);

            if (result == expectedResult)
            {
                Console.WriteLine("{0} was parsed correctly", toTest);
            }
            else
            {
                Console.WriteLine("*** {0} was NOT parsed correctly and returned {1} ***", toTest, result);
            }
        }

        private static TestResult SimulateEnterkeyPressed(string toTest)
        {
            if (string.IsNullOrWhiteSpace(toTest))
            {
                return TestResult.DoNothing;
            }

            // Uri.TryCreate will treat this as valid
            if (toTest.Equals("about:blank"))
            {
                return TestResult.DoNothing;
            }

            Uri url;
            if (Uri.TryCreate(toTest, UriKind.Absolute, out url))
            {
                return TestResult.TryCreatePass;
            }
            else
            {
                return TryParse(toTest);
            }
        }

        private static TestResult TryParse(string toTest)
        {
            if (!toTest.Contains("."))
            {
                return TestResult.Search;
            }

            if (!toTest.StartsWith("http://") ||
                !toTest.StartsWith("https://"))
            {
                return TestResult.ValidWithPrefix;
            }

            return TestResult.Unknown;
        }
    }
}

答案 3 :(得分:0)

终于对Matt Lacey的回答有了一个想法。只需在Navigate中添加一个if语句。

        if (!address.Contains("."))
        {
            address = " ";
            browsers[this.currentIndex].Navigate(new Uri("http://www.google.com/search?q=" + UrlTextBox.Text, UriKind.Absolute));
        }

关键是,如果我没有添加 address =“”; ,那么UrlTextBox会自动填充 http://