CefSharp Offscreen Example funktioniert nicht

  • C#

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von mrMo.

    CefSharp Offscreen Example funktioniert nicht

    Guten Tag, liebe Gemeinde,

    ich habe versucht, wieder via C# das CefSharp zu benutzen. Leider scheitere ich bereits am Example:

    github.com/cefsharp/CefSharp/b…Screen.Example/Program.cs

    Den Example wollte ich in mein Windows Forms C# Projekt hinzufügen und ich kriege eine Fehlermeldung beim await LoadPageAsync(browser);
    Schweregrad Code Beschreibung Projekt Datei Zeile Unterdrückungszustand
    Fehler CS0103 Der Name "LoadPageAsync" ist im aktuellen Kontext nicht vorhanden.

    Hier mein ganzer Code:

    Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. using CefSharp;
    11. using CefSharp.OffScreen;
    12. using System.Diagnostics;
    13. namespace Sharp_Offscreen_3
    14. {
    15. public partial class Form1 : Form
    16. {
    17. private const string TestUrl = "https://www.google.com/";
    18. public Form1()
    19. {
    20. Cef.Initialize();
    21. Console.ReadKey();
    22. Cef.Shutdown();
    23. }
    24. private void Form1_Load(object sender, EventArgs e)
    25. {
    26. }
    27. private static async void MainAsync(string cachePath, double zoomLevel)
    28. {
    29. var browserSettings = new BrowserSettings();
    30. //Reduce rendering speed to one frame per second so it's easier to take screen shots
    31. browserSettings.WindowlessFrameRate = 1;
    32. var requestContextSettings = new RequestContextSettings { CachePath = cachePath };
    33. // RequestContext can be shared between browser instances and allows for custom settings
    34. // e.g. CachePath
    35. using (var requestContext = new RequestContext(requestContextSettings))
    36. using (var browser = new ChromiumWebBrowser(TestUrl, browserSettings, requestContext))
    37. {
    38. if (zoomLevel > 1)
    39. {
    40. browser.FrameLoadStart += (s, argsi) =>
    41. {
    42. var b = (ChromiumWebBrowser)s;
    43. if (argsi.Frame.IsMain)
    44. {
    45. b.SetZoomLevel(zoomLevel);
    46. }
    47. };
    48. }
    49. await LoadPageAsync(browser);
    50. //Check preferences on the CEF UI Thread
    51. await Cef.UIThreadTaskFactory.StartNew(delegate
    52. {
    53. var preferences = requestContext.GetAllPreferences(true);
    54. //Check do not track status
    55. var doNotTrack = (bool)preferences["enable_do_not_track"];
    56. Debug.WriteLine("DoNotTrack:" + doNotTrack);
    57. });
    58. var onUi = Cef.CurrentlyOnThread(CefThreadIds.TID_UI);
    59. // For Google.com pre-pupulate the search text box
    60. await browser.EvaluateScriptAsync("document.getElementById('lst-ib').value = 'CefSharp Was Here!'");
    61. // Wait for the screenshot to be taken,
    62. // if one exists ignore it, wait for a new one to make sure we have the most up to date
    63. await browser.ScreenshotAsync(true).ContinueWith(DisplayBitmap);
    64. await LoadPageAsync(browser, "http://github.com");
    65. //Gets a wrapper around the underlying CefBrowser instance
    66. var cefBrowser = browser.GetBrowser();
    67. // Gets a warpper around the CefBrowserHost instance
    68. // You can perform a lot of low level browser operations using this interface
    69. var cefHost = cefBrowser.GetHost();
    70. //You can call Invalidate to redraw/refresh the image
    71. cefHost.Invalidate(PaintElementType.View);
    72. // Wait for the screenshot to be taken.
    73. await browser.ScreenshotAsync(true).ContinueWith(DisplayBitmap);
    74. }
    75. }
    76. }
    77. }


    Meine Fragen: Was mache ich konkret falsch? Wie kann ich das Example fehlerfrei übertragen?

    Ich bedanke mich recht herzlich und freundliche Grüße
    Semi
    In deinem Code fehlt die LoadPageAsync Methode. In der Doku steht sie drin, musste da mal genau schauen.
    "Gib einem Mann einen Fisch und du ernährst ihn für einen Tag. Lehre einen Mann zu fischen und du ernährst ihn für sein Leben."

    Wie debugge ich richtig? => Debuggen, Fehler finden und beseitigen
    Wie man VisualStudio nutzt? => VisualStudio richtig nutzen

    mrMo schrieb:

    In deinem Code fehlt die LoadPageAsync Methode. In der Doku steht sie drin, musste da mal genau schauen.


    Ich habe das umgehend korrigiert, das Script funktioniert leider nicht :/

    Da ich kein C#-Experte bin, weiß ich nicht ,was aktuell das Problem ist, wieso das Beispiel weiterhin nicht funktioniert, habs vollständig angepasst:

    C#-Quellcode

    1. using CefSharp;
    2. using CefSharp.Internals;
    3. using CefSharp.OffScreen;
    4. using System;
    5. using System.Collections.Generic;
    6. using System.ComponentModel;
    7. using System.Data;
    8. using System.Drawing;
    9. using System.Linq;
    10. using System.Text;
    11. using System.Threading.Tasks;
    12. using System.Windows.Forms;
    13. namespace CefSharp_4_SHARP
    14. {
    15. public partial class Form1 : Form
    16. {
    17. private const string TestUrl = "https://www.google.com/";
    18. public Form1()
    19. {
    20. InitializeComponent();
    21. Console.WriteLine("This example application will load {0}, take a screenshot, and save it to your desktop.", TestUrl);
    22. Console.WriteLine("You may see a lot of Chromium debugging output, please wait...");
    23. Console.WriteLine();
    24. }
    25. private static async void MainAsync(string cachePath, double zoomLevel)
    26. {
    27. var browserSettings = new BrowserSettings();
    28. //Reduce rendering speed to one frame per second so it's easier to take screen shots
    29. browserSettings.WindowlessFrameRate = 1;
    30. var requestContextSettings = new RequestContextSettings { CachePath = cachePath };
    31. // RequestContext can be shared between browser instances and allows for custom settings
    32. // e.g. CachePath
    33. using (var requestContext = new RequestContext(requestContextSettings))
    34. using (var browser = new ChromiumWebBrowser(TestUrl, browserSettings, requestContext))
    35. {
    36. if (zoomLevel > 1)
    37. {
    38. browser.FrameLoadStart += (s, argsi) =>
    39. {
    40. var b = (ChromiumWebBrowser)s;
    41. if (argsi.Frame.IsMain)
    42. {
    43. b.SetZoomLevel(zoomLevel);
    44. }
    45. };
    46. }
    47. await LoadPageAsync(browser);
    48. //Check preferences on the CEF UI Thread
    49. await Cef.UIThreadTaskFactory.StartNew(delegate
    50. {
    51. var preferences = requestContext.GetAllPreferences(true);
    52. //Check do not track status
    53. var doNotTrack = (bool)preferences["enable_do_not_track"];
    54. // Debug.WriteLine("DoNotTrack:" + doNotTrack);
    55. });
    56. var onUi = Cef.CurrentlyOnThread(CefThreadIds.TID_UI);
    57. // For Google.com pre-pupulate the search text box
    58. await browser.EvaluateScriptAsync("document.getElementById('lst-ib').value = 'CefSharp Was Here!'");
    59. // Wait for the screenshot to be taken,
    60. // if one exists ignore it, wait for a new one to make sure we have the most up to date
    61. await LoadPageAsync(browser, "http://github.com");
    62. browser.GetBrowser().MainFrame.ViewSource();
    63. string message = "You did not enter a server name. Cancel this operation?";
    64. string caption = "Error Detected in Input";
    65. MessageBoxButtons buttons = MessageBoxButtons.YesNo;
    66. DialogResult result;
    67. // Displays the MessageBox.
    68. result = MessageBox.Show(message, caption, buttons);
    69. //Gets a wrapper around the underlying CefBrowser instance
    70. var cefBrowser = browser.GetBrowser();
    71. // Gets a warpper around the CefBrowserHost instance
    72. // You can perform a lot of low level browser operations using this interface
    73. var cefHost = cefBrowser.GetHost();
    74. //You can call Invalidate to redraw/refresh the image
    75. cefHost.Invalidate(PaintElementType.View);
    76. // Wait for the screenshot to be taken.
    77. }
    78. }
    79. public static Task LoadPageAsync(IWebBrowser browser, string address = null)
    80. {
    81. //If using .Net 4.6 then use TaskCreationOptions.RunContinuationsAsynchronously
    82. //and switch to tcs.TrySetResult below - no need for the custom extension method
    83. var tcs = new TaskCompletionSource<bool>();
    84. EventHandler<LoadingStateChangedEventArgs> handler = null;
    85. handler = (sender, args) =>
    86. {
    87. //Wait for while page to finish loading not just the first frame
    88. if (!args.IsLoading)
    89. {
    90. browser.LoadingStateChanged -= handler;
    91. //This is required when using a standard TaskCompletionSource
    92. //Extension method found in the CefSharp.Internals namespace
    93. tcs.TrySetResultAsync(true);
    94. }
    95. };
    96. browser.LoadingStateChanged += handler;
    97. if (!string.IsNullOrEmpty(address))
    98. {
    99. browser.Load(address);
    100. }
    101. return tcs.Task;
    102. }
    103. private void Form1_Load(object sender, EventArgs e)
    104. {
    105. Cef.Initialize();
    106. string message = "You did not enter a server name. Cancel this operation?";
    107. string caption = "Error Detected in Input";
    108. MessageBoxButtons buttons = MessageBoxButtons.YesNo;
    109. DialogResult result;
    110. // Displays the MessageBox.
    111. result = MessageBox.Show(message, caption, buttons);
    112. }
    113. private void button1_Click(object sender, EventArgs e)
    114. {
    115. }
    116. }
    117. }
    Wenn ich nicht weiß warum etwas nicht funktioniere, debugge ich meinen Code. Heißt: Ich setzte einen Haltepunkt und steppe Schrittweise durch den Code. Hierbei schaue ich welche Werte meine Variablen haben und was der Code so macht. Das, mein sehr junger Padawan, ist nun deine Aufgabe.
    -> Debuggen, Fehler finden und beseitigen
    "Gib einem Mann einen Fisch und du ernährst ihn für einen Tag. Lehre einen Mann zu fischen und du ernährst ihn für sein Leben."

    Wie debugge ich richtig? => Debuggen, Fehler finden und beseitigen
    Wie man VisualStudio nutzt? => VisualStudio richtig nutzen