I’ve been working on a project in WPF C# and I’m trying to animate an image to move down. I have found the «MoveTo» function on the Internet and when I pasted it in the code the error occurred.
Public partial class Window1: Window
{
public static int w = 1;
public Window1()
{
InitializeComponent();
}
public void MoveTo(this Image target, double newY)
{
var top = Canvas.GetTop(target);
TranslateTransform trans = new TranslateTransform();
target.RenderTransform = trans;
DoubleAnimation anim1 = new DoubleAnimation(top, newY - top, TimeSpan.FromSeconds(10));
trans.BeginAnimation(TranslateTransform.XProperty, anim1);
}
private void button_Click(object sender, RoutedEventArgs e)
{
MoveTo(image, 130);
}
}
What I need to do to fix this?
kayess
3,3849 gold badges29 silver badges45 bronze badges
asked Jan 24, 2017 at 10:36
4
public void MoveTo(this Image target, double newY)
this
on the first argument of a method definition indicates an extension method which, as the error message says, only makes sense on a non-generic static class. Your class isn’t static.
This doesn’t seem to be something that makes sense as an extension method, since it’s acting on the instance in question, so remove the this
.
answered Jan 24, 2017 at 11:36
Jon HannaJon Hanna
109k9 gold badges143 silver badges249 bronze badges
1
MoveTo is an extension method — it’s just a syntactic sugar for a static function, so you can call
image.MoveTo(2.0)
instead of
SomeUtilityClass.MoveTo(image, 2.0)
However extension methods must be placed in a static class, so you cannot place it in your Window class. You can just omit «this» keyword in the declaration and use it like a static method or you need to move the method to a static class.
answered Jan 24, 2017 at 10:41
1
Please google first next time
https://msdn.microsoft.com/en-us/library/bb397656.aspx
Extension methods needs to be defined in a static class.
Remove the this keyword from the method signature «MoveTo».
this:
public void MoveTo(this Image target, double newY)
should be like this:
public void MoveTo(Image target, double newY)
answered Jan 26, 2017 at 9:02
shahar eldadshahar eldad
8611 gold badge12 silver badges30 bronze badges
Add keyword static to class declaration:
public static class ClassName{}
answered Jan 24, 2017 at 10:57
NagNag
6891 gold badge5 silver badges15 bronze badges
1
- Remove From My Forums
-
Question
-
I added a new page to my «views» now a page that I have not changed will not compile.
I commented out all references to my new page, but the error persists!
I ran windiff, and the only differences are alignment ( height, width) numbers.
the offending ?! line is:
namespace VCI_Envision_Portal.Views
{
public partial class SysConfig : Page
{It is the reference to ‘sysconfig’ that gets the complaint.
But I haven’t changed the definition! ( windiff confirms)
What gives?
Answers
All replies
-
Did you define any extension methods in SysConfig?
-
I’m not sure about «extensions» but I have narrowed it down:
namespace VCI_Envision_Portal.Views
{
public partial class SysConfig : Page
{
public SysConfig()
{InitializeComponent();
this.DataContext = this;
//ConfBGImage = App.GlobalVariables.WelcomeBackgroundImage;
//ConfSiteImage = App.GlobalVariables.SiteImage;
//ConfVCIImage = App.GlobalVariables.VCIImage;
}
/*
private static byte[] ToByteArray(this WriteableBitmap bmp)
{
int[] p = bmp.Pixels;
int len = p.Length * 4;
byte[] result = new byte[len];
// ARGB
Buffer.BlockCopy(p, 0, result, 0, len);
return result;
}
* */
}
}If I comment out the ToByteArray function, it compiles! I am at a loss!
especially when the compiler complains about the line: public partial class SysConfig : Page
gty7766 0 / 0 / 0 Регистрация: 18.06.2019 Сообщений: 10 |
||||
1 |
||||
27.11.2019, 17:13. Показов 1432. Ответов 4 Метки нет (Все метки)
Возникает ошибка при компиляции: error CS1106: Extension method must be defined in a non-generic static class
Вот весь код, что делать не знаю
__________________
0 |
Programming Эксперт 94731 / 64177 / 26122 Регистрация: 12.04.2006 Сообщений: 116,782 |
27.11.2019, 17:13 |
4 |
letsmail9 18 / 16 / 0 Регистрация: 03.01.2018 Сообщений: 208 Записей в блоге: 1 |
||||
27.11.2019, 18:21 |
2 |
|||
0 |
0 / 0 / 0 Регистрация: 18.06.2019 Сообщений: 10 |
|
27.11.2019, 19:12 [ТС] |
3 |
В unity я пока что новичок, да и с c# дела так себе. по этому мне нужна помощь ещё раз.
0 |
3088 / 1617 / 921 Регистрация: 26.10.2018 Сообщений: 4,620 |
|
28.11.2019, 10:17 |
4 |
gty7766, ну так а зачем ты его вложил в другой класс?
0 |
letsmail9 18 / 16 / 0 Регистрация: 03.01.2018 Сообщений: 208 Записей в блоге: 1 |
||||
29.11.2019, 10:35 |
5 |
|||
gty7766,
0 |
|
Just in case you come around this too: I just added a C# method to my DLL with other C# methods. But when I compiled the solution, I got: Error CS1106 Extension method must be defined in a non-generic static class This also caused the error Metadata file ‘.dll’ could not be found, which is Visual Studio slang for: there is a compiler error in that dl lso we can’t comile dll’s including that… First issue is that the «Extensions method» error points to the top of my program, where the class starts. If I hadn’t just inserted that method myself, which caused the error, I think there was no way that I could find the cause of the error! Second, I didn’t understand why other methods looking the same worked and this one didn’t. I already removed static (no need for that that I could think of) and after a long time of trying, found that my method said: public bool IsDataRowEmpty(this DataRow dr) Removing «this» solved the problem. Took me well over an hour to find it. Did I ever write I hate every second I work in VS? Oh yes, I did. Dick |
Please Log in or Create an account to join the conversation. |
|
Hi Dick, This is a problem indeed, but it does not have to do with VS, it’s a problem in the c# compiler. If you compile the same c# code in XIDE, you will get the same behavior and there’s nothing XIDE (or VS) that can do about it.
I checked and indeed the compiler error points to the class, not to the incorrectly defined method, which makes it extremely difficult to find the cause of the problem. We did have (or maybe still have) several such problems in X#, but they were fixed after people reported them. Try reporting this to MS, who knows maybe you will get lucky this time
PS. But of course it will help if you start your message to MS with «Hello, I think I found a problem in the c# compiler», instead of «This %*$&# Visual Studio is total %&^*#@ and I lost half my day trying to find my way around this &*^&^*@ problem that I will report to you», even though I agree it is very tempting to use version #2 XSharp Development Team |
Please Log in or Create an account to join the conversation. Last edit: by Chris. |
|
Hello Chris,
Hmm, I indeed always select option 2 The problem with most issues reported to Microsoft, even if phrased very politely, is that they close most of them with «Not enough info to…» even if the person who reported couldn’t state it more clearly. But I’ll give it a try. And include the totally unusable compiler error caused by ‘this’. Dick |
Please Log in or Create an account to join the conversation. |
|
Hello Dick, I recreated your problem in c#. As you said, i’ll get the CS1106 error which links to this page: docs.microsoft.com/en-us/dotnet/csharp/m…slyn%26k%3Dk(CS1106) With the knowledge, that the «this» keyword before the first parameter of a static method is used to define a extension method (which IMHO is an awesome contruct for certain usecases) and the example on the page, it should not take long to understand the problem. If I do the same in x# code, the error page just states «Extension method must be defined in a non-generic static class». Sugestion for @Chris: It might be sometimes helpful, that the xs-compiler-error-pages for all the roslyn-compiler errors have a link at the bottom to the corrisponding cs-compiler-error-page. Volkmar |
Please Log in or Create an account to join the conversation. Last edit: by VR. |
|
Chris,
Yes Chris, why don’t you do that ?
Volkmar, Robert XSharp Development Team |
Please Log in or Create an account to join the conversation. |
|
Hello Volkmar,
Probably Microsoft gives your more information on this URL than they give me The code was from a StackOverflow sample (and included ‘this’). I wish Microsoft transferred half of their bullshit writers to writing useful help pages. Dick |
Please Log in or Create an account to join the conversation. Last edit: by ic2. |
|
Nice typo Regards |
Please Log in or Create an account to join the conversation. |
|
Hello Karl, Ah yes, the type is actually the suggestion in my browser as it showed red underlined, so it was a type and I changed it into something totally different because I didn’t read what the suggestion was haha.
I’ll change it for now and anyone reading this later can see what it was in your reaction Dick |
Please Log in or Create an account to join the conversation. |
|
Dick
I know that MS has a quite large team working on the Docs.
During our meeting I suggested that they should add functionality to the website to write example code in «pseudo» language and to use a source code generator to then generate examples in C#, VB, F#, C++ etc. And of course to also allow 3rd parties (X#) to contribute with their own code generator. In other words: The developers and documentation writers at Microsoft are completely separated teams. Robert XSharp Development Team |
Please Log in or Create an account to join the conversation. |
|
Hello Robert, I met several nice people at Microsoft too. And I was even once asked by a senior VS developer to get in touch after I filled in the evaluation form. That was a surprise. I asked him to watch and see me working with VO and then I performed the same task in VS, where everything was much slower, I couldn’t find the last modified code like in VO with 2 mouse clicks and a small error in an entity did not get me 500 errors in all other entities. He was quite impressed with our old VO («Wow, that is indeed lightning fast») but I have not seen a single development since in VS closing that gap (not did I really expect so). The poor documentation like for that error CS1106 is an example that there may be a large independent documentation staff but that doesn’t necessarily have a positive result. Together with what you write it supports my idea that Microsoft consists of 100’s of smaller groups who hardly communicate with each other and almost none of them perform a job comparable with what you do with X#. Does this mean X# is perfect? No, neither is my or probably any software. I hardly ever install an upgrade immediately and await the first reported bugs to be solved. For X#, most are solved in that next version which also often follows the previous within days or weeks. With Microsoft software, severe bugs are solved soon too (but: we come around bugs in W10, Office, from which you think «how could this ever have been released») but many bugs are never solved. So yes, there are some clever guys or good teams. But the total picture is absolutely not in line with a software company with their resources. Imagine you had the budget of one of their smaller teams. X# would not have just been a very clever environment, but probably the best compiler and language and (x)IDE ever written. Dick |
Please Log in or Create an account to join the conversation. |